Laravel 5.8 Email verification step by step

Laravel Guy
4 min readJul 5, 2019

--

Step by step email verification

This article was originally posted on https://laravelguy.com/laravel-58-email-verification-step-by-step/

In this article “Laravel 5.8 Email verification step by step” we will discuss about how to verify user email with laravel 5.8. In our daily development we usually need the user verify their email before access to functionality of our application or website.

In the previous version of laravel we have to implement all the process of email verification manually but since the laravel 5.8 , Laravel provide a quick way to implement it.

Let’s take a look and see how quick we can make it.

Prerequisite

To implement the Email verification with laravel we have to get a working installation of laravel 5.8 and already make the authentication. If you do not have all of this you can read about them in those link.

Complete guide to install Laravel 5.8 on LinuxHow to setup laravel authentication in few simple step

Step 1 — Email configuration

To send email we need to configure our email in .env files. We could use any popular email service provider like Gmail, Yahoo or other but as we are testing we gonna use a service made for that.

Mailtrap.io

So mailtrap is basically a service that we will capture our email when we send it so we do need to spam real email inbox for our testing.

In order to register go to https://mailtrap.io and signup and after the signup the home page should look like this :

Home page Mailtrap.io

Then click on the Demo Inbox to get the credentials needed to configure laravel app for sending email.

Mailtrap.io credentials

Now we gonna edit our .env file to add the credentials.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=username_on_mailtrap
MAIL_PASSWORD=password_on_mail_trap
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=from@example.com
MAIL_FROM_NAME=Example

Our configuration is ready now we can go to the next step.

Step 2 — Email verification Setup

In this step we gonna do the email verification setup. So basically we will edit the User.php file to implement verify in the class. Edit the file App/User.php and implement MustVerify.

<?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', 'username'
];

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

Step 3 — Protecting Routes

Now we are ready to use verification email for the new registered user.

Laravel includes the Auth\VerificationController class that contains the necessary logic to send verification links and verify emails. To register the necessary routes for this controller, pass the verify option to the Auth::routes method: Go to routes/web.php and edit Auth::routes().

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

Route middleware can be used to only allow verified users to access a given route. Laravel ships with a verified middleware, which is defined at Illuminate\Auth\Middleware\EnsureEmailIsVerified. Since this middleware is already registered in your application’s HTTP kernel, all you need to do is attach the middleware to a route definition.

Route::get('profile', function () {
// Only verified users may enter...
})->middleware('verified');

Step 4 — Test

Now let test our setup. Our home page look like this :

Register page

After the registration the system send us a link to verify the email.

Go to mailtrap to click on the link in order to verify that email.

After that we can login normally with a verified email.

Here is the end of our article on Laravel 5.8 Email verification step by step.

Conclusion

There is more configuration you can do when you implement it on your website or application. First of all you can edit the mail template to make a beautiful one. Secondly you can make the redirection to another page after the user verify the email. Lastly when you are ready to deploy your application you will use a proper email than mailtrap and use proper encryption too like tls. Thank you for reading if you have any issue following this article please leave it in the comment i will try to help you.

--

--

Laravel Guy

I am a simple guy who love laravel and want to share knowledge and learn about it. You can read the blog et https://laravelguy.com