Modern Authentication System in Laravel with Email Verification & Password Reset
Installing Laravel Breeze
Laravel Breeze gives you a minimal, modern authentication scaffolding with Blade templates.
Use Composer to install it quickly.
Code Example (Terminal):
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
Configuring .env and Mail Settings
For email verification and password reset, set your mail configuration.
Code Example (.env):
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourmail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=no-reply@surisnippet.com
MAIL_FROM_NAME="${APP_NAME}"
Registration Form
Breeze includes a pre-built register.blade.php, but you can customize it.
Code Example (Blade):
<form method="POST" action="{{ route('register') }}">
@csrf
<input type="text" name="name" placeholder="Full Name" required>
<input type="email" name="email" placeholder="Email Address" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Register</button>
</form>
Login Form
Allows users to access protected routes after registration.
<form method="POST" action="{{ route('login') }}">
@csrf
<input type="email" name="email" placeholder="Email Address" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
Controller Logic
Laravel handles the heavy lifting, but you can manually manage auth if needed.
Code Example (Controller Snippet):
use Illuminate\Support\Facades\Auth;
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials, $request->filled('remember'))) {
$request->session()->regenerate();
return redirect()->intended('dashboard');
}
return back()->withErrors(['email' => 'Invalid credentials.']);
}
Enabling Email Verification
Add the MustVerifyEmail interface to your User model.
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
}
Protecting Routes with Middleware
Force users to verify their email before accessing protected pages.
Code Example (web.php):
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified']);
Enabling Email Verification
Add the MustVerifyEmail interface to your User model.
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
}
Protecting Routes with Middleware
Force users to verify their email before accessing protected pages.
Code Example (web.php):
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified']);
Customizing Verification Email
You can publish and customize the verification email template.
php artisan vendor:publish --tag=laravel-notifications
Forgot Password Form
Users can request a reset link sent to their email.
<form method="POST" action="{{ route('password.email') }}">
@csrf
<input type="email" name="email" placeholder="Enter your email" required>
<button type="submit">Send Reset Link</button>
</form>
Reset Password Form
The reset form appears when the user clicks the email link.
<form method="POST" action="{{ route('password.update') }}">
@csrf
<input type="hidden" name="token" value="{{ $request->token }}">
<input type="email" name="email" value="{{ $request->email }}" required>
<input type="password" name="password" placeholder="New Password" required>
<button type="submit">Reset Password</button>
</form>
Controller Logic for Password Reset
Code Example (PasswordResetController):
use Illuminate\Support\Facades\Password;
public function sendResetLink(Request $request)
{
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink($request->only('email'));
return $status === Password::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
Restricting Auth Routes
Use middleware to protect user-only pages.
Route::middleware(['auth'])->group(function () {
Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
});
Logging Out Securely
Always invalidate the session on logout.
Route::post('/logout', function (Request $request) {
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
})->name('logout');
Tailwind Form Styles
Make your forms look clean and responsive.
<input class="border rounded w-full p-2 focus:ring focus:ring-indigo-200" type="email">
<button class="w-full bg-indigo-600 text-white py-2 rounded mt-3 hover:bg-indigo-700">
Login
</button>
Test Scenarios
- Register โ Check email โ Verify โ Login
- Forgot password โ Reset โ Login again
Run locally:
php artisan serve
to test your flow.
English
Dutch