In this tutorial you will learn about the Laravel 5.8 New Email Verification and its application with practical example.
Laravel 5.8 Email Verification
Laravel 5.8 provides built-in email verification system for newly registered user. Using this, newly registered user will get an email with an account activation link. This activation link is used for account verification. When activation link is clicked, this will make user account verified and active for the application. Once user account is activated then it will be allowed to access protected routes; which can be accessible only by verified accounts.
In this step by step tutorial, we’ll show you how to setup email verification system for newly registered user in laravel 5.8.
Install Laravel 5.8
First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel larablog |
Make sure you have composer installed. Now, lets switch to the project directory and start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/
Output:-
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara58blog DB_USERNAME=root DB_PASSWORD= |
Now, use the below command to create default laravel tables automatically.
1 |
php artisan migrate |
Email Configuration
Since, we will be sending an email with an account activation link. To send email, we need to add email configuration or smtp details in .env file.
1 2 3 4 5 6 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=youremail@gmail.com MAIL_PASSWORD=yourpass MAIL_ENCRYPTION=tls |
Authentication Scaffolding
Now, use the below command to generate authentication scaffolding –
1 |
php artisan make:auth |
This command will generate required Controller files, views and add routes in our web.php routes file that are required for the authentication. If you open your route file (resources/routes/web.php) you will have a resource route added in it like below –
1 |
Auth::routes(); |
Email Verification Setup
To implement email verification we need to implement the MustVerifyEmail Contracts in our User model. Lets open the App/User.php file and add the “Implements MustVerifyEmail” to the class declaration as given below –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?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', ]; } |
Add Route
Last thing we need to do is add the verify middleware to the auth routes.
1 2 3 4 5 6 7 8 |
Route::get('/', function () { return view('welcome'); }); //This will enable email verification routes Auth::routes(['verify' => true]); Route::get('/home', 'HomeController@index')->name('home'); |
The “verified” middleware is used to protect routes from unverified users.
We can add the “verified” middleware to the route itself as given below –
Example:-
1 |
Route::get('/home', 'HomeController@index')->name('home')->middleware('verified'); |
or we can also use it in controllers as following –
Example:-
1 2 3 4 |
<?php public function __construct(){ $this->middleware(['auth','verified']); } |
Add Middleware In Controller
In this example we will be adding Middleware in controller’s constructor to protect access. Lets open app/controllers/HomeController.php and put $this->middleware([‘auth’, ‘verified’]); inside of constructor.
app/controllers/HomeController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?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\Http\Response */ public function index() { return view('home'); } } |
This will block unverified users to access laravel 5.8 dashboard, and only allow when users have verified their email. Now when you register a new user, it will send a verification email to that user and redirect to verify view (same will happen when a old user tries to access home route).
Start Development Server
Start the development server using following artisan command –
1 |
php artisan serve |
Test Laravel 5.8 Email Verification
At this stage, we are ready to test the user registration and login system with email verification system.
Now, you should able to see your registration page at –
http://localhost:8000/register
When you fill the form and submit it; you will get an alert for email verification in your account as follows –
You will also get an account verification link in your email account as follows –
Go ahead and verify your account, on successful verification you will be allowed to access protected application routes.