Laravel 6 Email Verification Tutorial

Laravel 6 Email Verification Tutorial

In this post, you'll learn how to add an email verification in Laravel 6

Originally published at https://www.techiediaries.com

Typically, after registering a user using an email address, you should see a verification email to verify that the user owns the email address they used for the registration of the account.

Only after successful email verification, the user will be authenticated and redirected to the home page.

If the user doesn't verify the email address, the app will display the message: “Before proceeding, please check your email for a verification link. If you did not receive the email.” and will disallow access to the home page.

We will make use of the new MustEmailVerify contract available in newer Laravel versions.

Let's implement this functionality in our CRM app.

In case, you didn't follow from the previous tutorial(s), these are the steps, you need to follow in a nutshell:

  • Create a Laravel 6 project using the composer create-project --prefer-dist laravel/laravel <name> command,
  • Set up your database in the .env file,
  • Install the laravel/ui package using the composer require Laravel/ui command,
  • Create the auth scaffolding using the php artisan ui vue --auth,
  • Run the npm install && npm run dev command to build the assets of the auth scaffolding such as JS and Bootstrap,
  • Migrate your database using the php artisan migrate command.

If you successfully executed the previous step, particularly the database and the auth scaffolding, you next need to add the SMTP credentials in the .env file.

First, if you don't already have an SMTP server, you can register a free account with mailtrap, a service for email testing in staging and development.

After you register for an account, you'll get your SMTP credentials once your access your inbox. Copy the username and password and add them in the MAIL_USERNAME and MAIL_PASSWORD options

MAIL_DRIVER=smtp 
MAIL_HOST=smtp.mailtrap.io 
MAIL_PORT=2525 
MAIL_USERNAME=<YOUR_MAILTRAP_USERNAME> 
MAIL_PASSWORD=<YOUR_MAILTRAP_PASSWORD> 
MAIL_ENCRYPTION=tls

Next, you need to implement the MustVerifyEmail contract in the User model. Open the App/User.php file and update as follows:

<?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;

   protected $fillable = [        'name', 'email', 'password',    ];

   protected $hidden = [        'password', 'remember_token',    ];

   protected $casts = [        'email_verified_at' => 'datetime',    ]; }

Next, open the app/routes/web.php file and add:

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

This will add routes like email/verify and email/resend to our application.

Next, open the app/Http/Controllers/HomeController.php file and add the verified middleware as follows:

class HomeController extends Controller
{

   public function __construct()    {        $this->middleware('verified');    }

Please note that we already have the auth middleware added in the home controller but we changed that in the previous tutorial to call it from the route instead. If you don't prefer to make that change, you can add the verified and auth middlewares using an array as follows:  

  public function __construct()
   {
       $this->middleware(['auth', 'verified']);
   }

That's it! We have now added email verification in our application. Go ahead and register a new account, you should be redirected to the following page that asks you to verify your email address before proceeding:

Next, head to your mailtrap inbox, you should find the verification email:

Open it and verify your account. You should be redirected to the home page.

The email template looks professional and doesn't even need any customizations except for the Laravel title. This is simply the name of our application and we can easily change it from the .env file:

APP_NAME=CRMApp 

This is a screenshot of the verification email, now:

Conclusion

In this tutorial, we've seen how to add email verification in our Laravel 6 application.  

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

What’s New in Laravel 6.0

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial

Laravel 6 Authentication Tutorial

Laravel 6 Social Login Tutorial - Login with Github





laravel security php

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

Best Custom Web & Mobile App Development Company

Top Web & Mobile Application Development Company in India & USA. We specialize in Golang, Ruby on Rails, Symfony, Laravel PHP, Python, Angular, Mobile Apps, Blockchain, & Chatbots

10 Laravel Quick Tips

Spread the loveTip 1. Controllers Having Single Action In some situations you need a single action in a controller, if this is the case in Laravel you can achieve it by __invoke() method. Routes: Artisan command to generate this controller:…Read More→