Laravel 9 Email Verification Example; In this tutorial we will show you you how to enable email email varification in laravel application. Email verification is one of the basic requirements of most applications. In Laravel application, we can easily setup email verification.
In admin panel when a new user is register the application we need sometimes the user will must be verify email address before accessing app features so we can see user a message to varify your email address.
How to Implement Email Verification System in laravel App
Follow the following steps to create email verification system in laravel 8, 9 applications:
- Step 1: Download Laravel App
- Step 2: Connect Database to App
- Step 3: Create Authentication System
- Step 4: Setup Email Configuration
- Step 5: Email Verification Setup
- Step 6: Configure Auth Routes
- Step 7: Run Application & Test Email Verification
Download Laravel App
First of all install a new laravel application using the following command. You can skp this steps if you have already laravel app installed.
composer create-project laravel/laravel email-varification
Connect Database to App
Open the .env file and add your database credentials such as database name, username equally important password:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database name here DB_USERNAME=database username here DB_PASSWORD=database password here
Create Authentication
Now generate Authentication Scaffolding using the following command:
For 6+ version
composer require laravel/ui
php artisan ui vue --auth
npm install
npm run dev
For 5.8 below version
php artisan migrate
Setup Email Configuration
Now update your database credential and email SMTP credentials in .env file. Here we are using https://mailtrap.io/ for testing purpose. You can also get demo credentials from mailtrap.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Email Verification Setup
For email varification setup you we need to open User model from app/User.php and implements MustVerifyEmail in User class just like below example:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Configure Auth Routes
Now we need to add varify true parameter inside Auth::routes(). Open routes/web.php and do the change like this:
Auth::routes(['verify' => true]);
Now to need to show the verify message when nuser logged in using the varified middleware to add in your homecontroller or your can use in your routes as well. Here our user is redireting in homecontroller so we have put the code in HomeController.php file.
public function __construct()
{
$this->middleware(['auth', 'verified']);
}
app\Http\Controllers\HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(['auth','verified']);
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
By this method, we can give access to verified users to any controller.
Run Application & Test Email Verification
Finally, we need to run the following PHP artisan serve command to start laravel livewire upload file app:
php artisan serve
Now open your browser and hit your project url http://localhost:8000/register.
After register using user you have go an email in your mailtrap account and you see below message in your dashboard, If you logged in as well.
After Click to request another your have got an other email in your mailtrap account just like below.
When you clicked on ‘Verify Email Address’, it redirected you the home page and the users table email_verified_at field has been updated. and the email varify message now showing again your.
That’s all buddy, I hope you found your best solution for laravel email varification example… Feel free to share and follow us on twitter or facebook.