Learn Laravel Authentication with Breeze/UI
Authentication is the process of verifying the identity of a user. It ensures that users are who they claim to be before granting access to protected resources.
| Authentication | Authorization |
|---|---|
| Who are you? | What can you do? |
| Verifies identity | Verifies permissions |
| Login/Register | Roles/Permissions |
| Example: User login | Example: Admin access |
Laravel Breeze is a minimal, simple implementation of all Laravel's authentication features including login, registration, password reset, email verification, and password confirmation.
Laravel UI provides Bootstrap scaffolding for authentication. Good if you prefer Bootstrap over Tailwind.
Jetstream includes more advanced features like team management, two-factor authentication, and API tokens.
# Create new Laravel project
composer create-project laravel/laravel auth-app
# Navigate to project
cd auth-app
# Install Breeze via Composer
composer require laravel/breeze --dev
# Install Breeze with Blade templates
php artisan breeze:install blade
# Install NPM dependencies
npm install
# Compile assets
npm run dev
What does breeze:install do?
# Edit .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=auth_app
DB_USERNAME=root
DB_PASSWORD=
# Create authentication database tables
php artisan migrate
Tables Created:
# Start Laravel server
php artisan serve
# In another terminal, compile assets
npm run dev
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\RegisteredUserController;
use Illuminate\Support\Facades\Route;
// Registration routes
Route::get('/register', [RegisteredUserController::class, 'create'])
->name('register');
Route::post('/register', [RegisteredUserController::class, 'store']);
// Login routes
Route::get('/login', [AuthenticatedSessionController::class, 'create'])
->name('login');
Route::post('/login', [AuthenticatedSessionController::class, 'store']);
// Logout route
Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
->name('logout');
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
// Mass assignable attributes
protected $fillable = [
'name',
'email',
'password',
];
// Hidden attributes (not shown in JSON)
protected $hidden = [
'password',
'remember_token',
];
// Attribute casting
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
# Register page
http://localhost:8000/register
# Login page
http://localhost:8000/login
# Dashboard (requires login)
http://localhost:8000/dashboard
Laravel uses bcrypt hashing algorithm to securely store passwords. Passwords are never stored in plain text!
use Illuminate\Support\Facades\Hash;
// Hash a password
$hashedPassword = Hash::make('password123');
// Verify a password
if (Hash::check('password123', $hashedPassword)) {
// Password matches
}
use Illuminate\Validation\Rules\Password;
// Basic password rules
'password' => ['required', 'confirmed', Password::min(8)],
// Strong password rules
'password' => [
'required',
'confirmed',
Password::min(8)
->letters()
->mixedCase()
->numbers()
->symbols()
],
| Column | Type | Description |
|---|---|---|
| id | BIGINT | Primary key |
| name | VARCHAR(255) | User's full name |
| VARCHAR(255) | Unique email address | |
| email_verified_at | TIMESTAMP | Email verification time |
| password | VARCHAR(255) | Hashed password |
| remember_token | VARCHAR(100) | Remember me token |
| created_at | TIMESTAMP | Registration time |
| updated_at | TIMESTAMP | Last update time |
Stores user session data for logged-in users.
Stores tokens for password reset requests.
<x-guest-layout>
<!-- Session Status -->
<x-auth-session-status class="mb-4" :status="session('status')" />
<form method="POST" action="{{ route('login') }}">
@csrf
<!-- Email Address -->
<div>
<x-input-label for="email" :value="__('Email')" />
<x-text-input id="email" class="block mt-1 w-full"
type="email" name="email" :value="old('email')"
required autofocus />
<x-input-error :messages="$errors->get('email')" class="mt-2" />
</div>
<!-- Password -->
<div class="mt-4">
<x-input-label for="password" :value="__('Password')" />
<x-text-input id="password" class="block mt-1 w-full"
type="password" name="password"
required />
<x-input-error :messages="$errors->get('password')" class="mt-2" />
</div>
<!-- Remember Me -->
<div class="block mt-4">
<label for="remember_me" class="inline-flex items-center">
<input id="remember_me" type="checkbox" name="remember">
<span class="ml-2">{{ __('Remember me') }}</span>
</label>
</div>
<div class="flex items-center justify-end mt-4">
<x-primary-button>
{{ __('Log in') }}
</x-primary-button>
</div>
</form>
</x-guest-layout>
<x-input-label>) for reusable UI elements.
Requirements:
In Day 2, we'll dive deeper into: