0
0
Laravelframework~5 mins

Email verification in Laravel

Choose your learning style9 modes available
Introduction

Email verification helps confirm that a user owns the email they provide. It improves security and trust by making sure users are real.

When users register on your website and you want to confirm their email address.
To prevent fake or spam accounts from being created.
Before allowing users to access certain features that require verified contact info.
When you want to send important notifications only to verified users.
Syntax
Laravel
use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
    // User model code
}
Implementing the MustVerifyEmail interface on your User model enables email verification features.
Laravel automatically sends a verification email when a user registers if this interface is used.
Examples
This example shows how to add email verification to the User model by implementing the MustVerifyEmail interface.
Laravel
<?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
}
This example shows the routes needed to handle email verification notices, verification links, and resending verification emails.
Laravel
<?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');
Sample Program

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.

Laravel
<?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');
OutputSuccess
Important Notes

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.

Summary

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.