Handling Email Verifcation in Laravel

Handling Email Verifcation in Laravel

When a user clicks on the Signup button of a website or application, he/she generally gets a confirmation email with an activation link. This is the most crucial part of a system. It is to ensure the user owns the email address which was entered during the Signup. After clicking on the activation link, the user is authenticated to use the application or website.

Email verification in Laravel is quite simple as it is available out-of-the-box from Laravel 5.7+. If you have the earlier versions of Laravel you can use an email verification package. In this article, we will look into email verification for Laravel 5.7+.


Create a new project as an example

Email verification required sending mail, so lets create a new project with the following command:

composer create-project --prefer-dist laravel/laravel app

Now, lets create a database in mysql and then configure the .env file.


DB_CONNECTION=mysql

DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=email-verification DB_USERNAME=root DB_PASSWORD=


Run the migration command to create users, password resets, and failed jobs.


Since, the Laravel app sends up a confirmation email, we need to configure the .env file. For testing purpose, dummy SMTP server can be used provided by the Mailtrap.io. You can sign-up and add your credentials to the .env.  

MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=********> //Your Mailtrap username MAIL_PASSWORD=********> //Your Mailtrap password MAIL_ENCRYPTION=tls


Laravel Scaffold UI

In Laravel you can use the package called laravel/uii which is responsible for login and registration scaffolding with Vue, React, jQuery and Bootstrap layouts. Enter the following command to scaffold UI with Vue.

php artisan ui vue --auth


Using MustVerify Contract

The Must Verify Contract in Laravel allows users to get a verification email. Add the following lines of code to the following files:


App/User.php

Impement MustVerify Contract in the User model:


namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'email_verified_at'
];

/**
* 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',
];

}


routes/web.php

Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify' => true]);
Route::get('/home', '[email protected]')->name('home');


You can also add verified middleware if your are using Route Prefix.


Route::get('/', function () {
return view('welcome');
});

Auth::routes(['verify' => true]);

Route::group(['prefix' => 'admin', 'middleware' => 'verified'], function () {
Route::get('/dashboard', 'Admin\[email protected]')->name('admin.dashboard');
});

Here, the verified middleware ensures that the user can only access the routes under the prefix admin only if he/she is verified. You can use this method to protect admin routes or any other routes.


app/Http/Controllers/HomeController.php

Add the verified and auth middleware.



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');
}
}


Now, the email verification has been successfully implemented. You app is ready for testing. Your email verification link will be on your Mailtrap Demo inbox. 

Recent comments(0)

Please, Sign In to leave a reply