Email Verification Example In Laravel, we’ll learn how to verify an email address after a user registers in a Laravel application. Laravel provides a built-in feature for email verification, which simplifies the process of ensuring users confirm their email before accessing certain parts of your app
Email Verification Example In Laravel
Many web applications need users to Email Verification before they can access the app. Instead of making you build this feature from scratch for every project, Laravel offers easy-to-use, built-in services for sending and verifying email verification requests.
Steps to Enable Email Verification Example in Laravel:
- Install Laravel (if not already done):
- Email Configuration
- Generate Authentication files.
- Model Setup.
- Routing
Step 1: Install Laravel Applications
If you already have a Laravel project, you can skip this step. Otherwise, run the following command in your terminal to create a new Laravel project:
create-project --prefer-dist laravel/laravel EmailVerificationApp
Step 2 Email And Database Configurations
Next, go to your .env
file and make the following changes.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=yourusername@gmail.com
MAIL_PASSWORD= yourapppassword
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS='yourusername@gmail.com'
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Generate Authentication files.
Laravel has built-in authentication features. To generate authentication scaffolding
You go here for more details:- Laravel application starter kits
We will be creating our authentication files using laravel ui
package
Please run following commad on terminal
composer require laravel/ui
php artisan ui bootstrap --auth
php artisan migrate
the commmand php artisan migrate
are migrating to our database users table
Step 4 : Model Setup.
Update the User
Model: Your User
model should implement the MustVerifyEmail
interface to enable email verification.
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
// model properties and methods...
}
Step 5 : Routing:
Go to your routes/web.php
. Lets first add the routes that redirect authenticated but unverified users to a view file. Notice that we attached an Auth middleware and our route name is verification.notice
.
Route::get('/email/verify', function () {
return view('auth.verify');
})->middleware('auth')->name('verification.notice');
You should not change the route name because our Auth setup uses Laravel’s built-in verified
middleware to automatically redirect users to that route if they haven’t verified their email address.
Email Verification Handler
The next route we will define is responsible for verifying the user’s email when they click the link sent to their inbox. This will also be added to your routes/web.php
// remember to import EmailVerificationRequest class
use Illuminate\Foundation\Auth\EmailVerificationRequest;
Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $r) {
$r->fulfill();
return redirect('/home');
})->middleware(['auth', 'signed'])->name('verification.verify');
What if the user deletes the email we sent or can’t find it? We want to give them the chance to request another verification email. Here’s how:
use Illuminate\Http\Request;
Route::post('/email/verification-notification', function (Request $r) {
$r->user()->sendEmailVerificationNotification();
return back()->with('resent', 'Verification link sent ');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');
Using this route via a post request, Laravel sends an email notification once again. You can find the responsible blade file in views\auth\verify.blade.php
Thanks For Reading. Hope This articled will help you?