Laravel 2 min read 444 views

How to Implement Email Verification in Laravel 11

Set up email verification for new user registrations with custom emails and resend functionality.

E
Email verification

Step 1: Implement MustVerifyEmail

use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
    // ...
}

Step 2: Add Routes

use Illuminate\Foundation\Auth\EmailVerificationRequest;

// Show verification notice
Route::get('/email/verify', function () {
    return view('auth.verify-email');
})->middleware('auth')->name('verification.notice');

// Handle verification link
Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect('/dashboard')->with('verified', true);
})->middleware(['auth', 'signed'])->name('verification.verify');

// Resend verification email
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');

Step 3: Protect Routes

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
});

Step 4: Verification Notice View

<div class="alert">
    <p>Please verify your email address.</p>

    @if (session('message'))
        <p class="success">{{ session('message') }}</p>
    @endif

    <form method="POST" action="{{ route('verification.send') }}">
        @csrf
        <button type="submit">Resend Verification Email</button>
    </form>
</div>

Step 5: Custom Verification Email

// app/Notifications/CustomVerifyEmail.php
class CustomVerifyEmail extends Notification
{
    public function toMail($notifiable)
    {
        $url = URL::temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60),
            ['id' => $notifiable->id, 'hash' => sha1($notifiable->email)]
        );

        return (new MailMessage)
            ->subject('Verify Your Email - ' . config('app.name'))
            ->greeting('Hello ' . $notifiable->name . '!')
            ->line('Click the button below to verify your email.')
            ->action('Verify Email', $url)
            ->line('This link expires in 60 minutes.');
    }
}

// In User model
public function sendEmailVerificationNotification()
{
    $this->notify(new CustomVerifyEmail);
}
Share this article:
ES

Written by Edrees Salih

Full-stack software engineer with 9 years of experience. Passionate about building scalable solutions and sharing knowledge with the developer community.

View Profile

Comments (0)

Leave a Comment

Your email will not be published.

No comments yet. Be the first to share your thoughts!