In this tutorial we will learn how laravel 5.7 email verification with activation code works and how laravel handles and activate user email verification with account activation.
Most of the websites now have this feature to prevent unauthorized and spam email access and legit user filtering with the help of account activation through email after registration in their website.
In the past we used to create our own email verification and activation system, luckily now latest version of laravel also supports email verification with activation code out of the box and we just need to enable the support. There is much more to see in the recent version of laravel which is now at this time of writing is 5.7. If you want to check out their recent documentation then click this link.
Even if it ships the feature out of the box, we still need to enable it making some tweaks inside the code. We will follow step by step tutorial below in order to unlock email activation feature in our laravel application.
Let’s get started.
What are we going to do ?
- Creating fresh copy of laravel with default authentication scaffolding.
- Implement MustVerifyEmail and configure routes.
- Understanding newly created files and Controllers.
- Conclusion.
Step 1 : Creating fresh copy laravel with Default Authentication Scaffolding
Go to command line terminal and enter php artisan make:auth
to get your laravel authentication ready.
New views have been created inside resources/views for login, registration and email activation or verification.
Step : 2 Implement MustVerifyEmail
Interface and Configure Routes
To enable email verification with activation support User.php
class needs to implement MustVerifyEmail
Interface.
Go to User.php
and implement MustVerifyEmail
Interface. Look at the code 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 // Implementing Interface { 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', ]; } |
Now, setting up the routes. Copy this code just below Auth::routes();
1 |
Auth::routes(['verify' => true]); |
Step 3 : Understanding Newly Controller and Middleware
In laravel 5.7 a new controller is introduced called VerificationController
inside App\http\Controllers\Auth
to handle the logic to send email verification with a link to the user after registration.
New Middleware class is introduced to handle the routes from unauthorized users and to ensure that the email is verified. This middleware can be found inside the kernel.php
and look for the registered middleware called ‘verified’. Take a look at the code below.
1 2 3 4 5 6 7 8 9 10 11 |
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, ]; |
Finally,we are going to use verified
middleware to only allow access to the users who are verified in the routes web.php
. Take a look at the sample below.
1 2 3 |
Route::get('/', function () { return view('welcome'); })->middleware('verified'); |
Conclusion
After successful verification the user is redirected to the /home
URL that is specified in the route and the VerificationController.php
. You can also customize the default redirection redirectTo
to redirect user to the different location that specified in the controller.
It is pretty straight forward if you have a look through the documentation, but this tutorial will help you to get started very easily and understand it step by step how the things works. If you have any problem mention them in comments or contact us through the contact page.
Leave a Reply