📖 Scenario: You are building a simple Laravel application that requires users to verify their email addresses after registration. This helps ensure that users provide valid emails before accessing protected parts of the app.
🎯 Goal: Create a Laravel controller and routes to handle sending email verification links and verifying users when they click the link.
📋 What You'll Learn
Create a controller named EmailVerificationController
Add a route to send the email verification notification
Add a route to verify the email using a signed URL
Use Laravel's built-in email verification features
💡 Why This Matters
🌍 Real World
Email verification is essential in real apps to confirm user identity and reduce fake accounts.
💼 Career
Understanding Laravel's email verification helps you build secure user authentication flows, a common requirement in web development jobs.
Progress0 / 4 steps
1
Create EmailVerificationController
Create a controller named EmailVerificationController inside app/Http/Controllers with a method sendVerificationEmail that accepts a Request parameter.
Laravel
Hint
Use Auth::user() to get the logged-in user and call sendEmailVerificationNotification() on it.
2
Add route to send verification email
Add a POST route in routes/web.php with URL /email/send-verification that uses EmailVerificationController@sendVerificationEmail and applies the auth middleware.
Laravel
Hint
Use Route::post with the URL and controller method, then chain middleware('auth').
3
Add route for email verification link
Add a GET route in routes/web.php with URL /email/verify/{id}/{hash} that uses VerificationController@verify and applies the auth and signed middleware.
Laravel
Hint
Use Route::get with the URL and controller method, add auth and signed middleware, and name the route verification.verify.
4
Create VerificationController with verify method
Create a controller named VerificationController inside app/Http/Controllers with a method verify that accepts Request $request. Inside, call $request->user()->markEmailAsVerified() and return a JSON response with message 'Email verified successfully.'.
Laravel
Hint
Use markEmailAsVerified() on the authenticated user and return a JSON message.
Practice
(1/5)
1. What is the main purpose of email verification in Laravel?
easy
A. To reset the user's password automatically
B. To delete unverified user accounts immediately
C. To log the user out after registration
D. To confirm a user's email address for security and trust
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 D
Quick Check:
Email verification = Confirm email ownership [OK]
Hint: Email verification confirms user email ownership [OK]
Common Mistakes:
Confusing email verification with password reset
Thinking it logs out users automatically
Assuming it deletes accounts immediately
2. Which interface must the User model implement to enable email verification in Laravel?
easy
A. Illuminate\Contracts\Auth\MustVerifyEmail
B. Illuminate\Contracts\Auth\Authenticatable
C. Illuminate\Contracts\Auth\CanResetPassword
D. Illuminate\Contracts\Auth\Access\Authorizable
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 A
Quick Check:
User model implements MustVerifyEmail [OK]
Hint: Use MustVerifyEmail interface on User model [OK]
Common Mistakes:
Using Authenticatable instead of MustVerifyEmail
Confusing with password reset interfaces
Missing the correct namespace
3. Given this route setup in Laravel:
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?
medium
A. They are redirected to the email verification notice page
B. They see the dashboard content immediately
C. They get a 404 Not Found error
D. They are logged out automatically
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 A
Hint: Implement MustVerifyEmail to enable verification [OK]
Common Mistakes:
Thinking extending Authenticatable is wrong
Assuming 'verified' attribute is required manually
Believing Notifiable trait controls verification
5. You want to customize the email verification process to send a verification email immediately after user registration. Which Laravel feature should you use to trigger this email automatically?
hard
A. Override the User model's boot method to send the email
B. Manually call the sendEmailVerificationNotification method in the controller
C. Use the Registered event and listen to it to send the verification email
D. Add a middleware to send the email on every request
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 C
Quick Check:
Registered event triggers verification email [OK]
Hint: Listen to Registered event to send verification email [OK]
Common Mistakes:
Calling sendEmailVerificationNotification manually in controller
Overriding boot method unnecessarily
Using middleware to send email on every request
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.