Email verification helps confirm that a user owns the email they provide. It improves security and trust by making sure users are real.
Email verification in Laravel
use Illuminate\Contracts\Auth\MustVerifyEmail;
class User extends Authenticatable implements MustVerifyEmail
{
// User model code
}<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Contracts\Auth\MustVerifyEmail; class User extends Authenticatable implements MustVerifyEmail { // User model code }
<?php // In routes/web.php use Illuminate\Foundation\Auth\EmailVerificationRequest; use Illuminate\Http\Request; Route::get('/email/verify', function () { return view('auth.verify-email'); })->middleware('auth')->name('verification.notice'); Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) { $request->fulfill(); return redirect('/home'); })->middleware(['auth', 'signed'])->name('verification.verify'); Route::post('/email/verification-notification', function (Request $request) { $request->user()->sendEmailVerificationNotification(); return back()->with('message', 'Verification link sent!'); })->middleware(['auth', 'throttle:6,1'])->name('verification.send');
This complete example shows how to enable email verification in Laravel by updating the User model and defining the necessary routes. When a user registers, Laravel sends a verification email automatically. The user visits the verification link to confirm their email.
<?php // User.php (Model) namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Contracts\Auth\MustVerifyEmail; class User extends Authenticatable implements MustVerifyEmail { // User model code } // routes/web.php use Illuminate\Foundation\Auth\EmailVerificationRequest; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::get('/email/verify', function () { return 'Please verify your email address.'; })->middleware('auth')->name('verification.notice'); Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) { $request->fulfill(); return 'Email verified successfully!'; })->middleware(['auth', 'signed'])->name('verification.verify'); Route::post('/email/verification-notification', function (Request $request) { $request->user()->sendEmailVerificationNotification(); return 'Verification link sent!'; })->middleware(['auth', 'throttle:6,1'])->name('verification.send');
Make sure your User model uses the MustVerifyEmail interface to enable verification.
Laravel's built-in email verification uses signed URLs to secure verification links.
Use middleware like 'auth' and 'signed' to protect verification routes.
Email verification confirms a user's email to improve security.
Implement MustVerifyEmail on the User model to enable it.
Set up routes to handle verification notices, verification links, and resending emails.
Practice
Solution
Step 1: Understand email verification purpose
Email verification ensures the user owns the email they provided.Step 2: Identify security benefits
This process improves security and trust by confirming email validity.Final Answer:
To confirm a user's email address for security and trust -> Option DQuick Check:
Email verification = Confirm email ownership [OK]
- Confusing email verification with password reset
- Thinking it logs out users automatically
- Assuming it deletes accounts immediately
Solution
Step 1: Identify the required interface for email verification
Laravel requires the User model to implement MustVerifyEmail to enable verification.Step 2: Confirm the correct namespace and interface
The interface is Illuminate\Contracts\Auth\MustVerifyEmail.Final Answer:
Illuminate\Contracts\Auth\MustVerifyEmail -> Option AQuick Check:
User model implements MustVerifyEmail [OK]
- Using Authenticatable instead of MustVerifyEmail
- Confusing with password reset interfaces
- Missing the correct namespace
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
return 'Welcome to your dashboard!';
});
What happens if a logged-in user tries to access /dashboard without verifying their email?Solution
Step 1: Analyze middleware effects
The 'verified' middleware checks if the user's email is verified before allowing access.Step 2: Determine behavior for unverified users
If not verified, Laravel redirects them to the verification notice page.Final Answer:
They are redirected to the email verification notice page -> Option AQuick Check:
'verified' middleware redirects unverified users [OK]
- Assuming unverified users see dashboard content
- Expecting a 404 error instead of redirect
- Thinking users get logged out automatically
class User extends Authenticatable
{
// Missing implementation here
}
Why might email verification fail to work properly with this model?Solution
Step 1: Check interface implementation
Email verification requires the User model to implement MustVerifyEmail interface.Step 2: Identify missing implementation
The snippet lacks this interface, so verification won't trigger properly.Final Answer:
Because the model does not implement MustVerifyEmail interface -> Option BQuick Check:
Missing MustVerifyEmail causes verification failure [OK]
- Thinking extending Authenticatable is wrong
- Assuming 'verified' attribute is required manually
- Believing Notifiable trait controls verification
Solution
Step 1: Identify Laravel's event system for registration
Laravel fires a Registered event after user registration.Step 2: Use event listener to send verification email
Listening to Registered event allows sending the verification email automatically.Final Answer:
Use the Registered event and listen to it to send the verification email -> Option CQuick Check:
Registered event triggers verification email [OK]
- Calling sendEmailVerificationNotification manually in controller
- Overriding boot method unnecessarily
- Using middleware to send email on every request
Ready to prove it?
Pass the test and this pattern moves unknown
