Enable Laravel 5.7 Email Verification Support

As we all know e-mail confirmation is a must-have feature for most of the web application, that being an essential need for the applications the Laravel has come across the out of the box inbuilt solution for us and we can really easily implement this into Laravel 5.7 projects.

So let’s get started:

Create new Laravel 5.7 Project


Database Setup:
Update database settings from .env file:

Next, migrate the database tables:

If you look at the database your should get tables created accordingly and most specifically check out the users table it has a new field added from Laravel that is email_verified_at.

Frontend Scaffolding
Use following command to generate default front views and routes.

From Laravel 5.7 the make:auth command has one more extra view added to the resources/views/auth/verify.blade.php. This is view user is going to get when they are logged in but not verified there email address.

Prepare Model to implement MustVerify Interface

Laravel 5.7 comes with a new interface for User model, but default it is not ejected into the class it is according to the project requirements. So, whether the project needs to have an email verification step or not.
So next will have update User model to implement MustVerifyEmailContract.

Enable Verification on Routing
Next will have to provide an extracted parameter to the Auth::routes(); to ask using email verification feature. Basically, it registers a new Verification controller with the route actions. The new controller is located under app/Http/Controllers/Auth/VerificationController.php. This controller is responsible for sending verification email messages and also resending the message if the user does not receive the email message.

Protect Routes
Laravel has new route middleware available for us to use when we enable email verification, to use that simply do the same as we always do when whenever we use middleware with the controller, add new verified middleware into the constructor as showing below:

If you don’t want user home as your default homepage you can always use the same middleware with your new route and you can also modify redirection path when the user gets successfully verified.

You can simply open VerificationController from Auth folder and update protected $redirectTo = ‘/somethingelse’; but in our case, we don’t need to do that as we are using the home as our default redirection page.

Email Driver Setup
Next, you will have set up email drivers for the project, there are several ways are provided in Laravel such as SMTP, Mailgun, Mandrill, Sparkpost, array, and log etc.

Update the .env file with your preferred email driver.

Test Email Verification Support
So let’s create new account see how does this implementation works:

Open register page and register a new user. You should get redirected to the verify view, where it says verify your email address. Check your email inbox for the verification email. Click verification link and you will be redirected to the home page.

Now if you checkout users table into the database you should see the new records added and email_verified_at has value.

Leave a Reply

Your email address will not be published. Required fields are marked *