Email Verification after Registration in Laravel

In this tutorial, i will share with you how to setup register user email verification in laravel 5.8. new user must be verify email address before logic in laravel 5.8. we will send activation code on register email address to verify email in laravel 5.8 app.

In laravel old version we are doing email verification process manually, but in laravel 5.8 they provide in build email verification setup for new registered users to must have to verify his email before proceed. You just need to make some basic setup with need to use middleware, routes and mail configuration.

Just follow this tutorial from scratch and you will set up for email verification in laravel 5.8 project.

Step 1: Install Laravel 5.8

First of all, we need to get fresh Laravel 5.8 version application using bellow command because we are going from scratch, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

In this step, we need to add database configuration details on .env file. So let's create username, password etc. So let's add.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=emailverify
DB_USERNAME=root
DB_PASSWORD=

After added database configuration, you need to run default migration of laravel by following command:

php artisan migrate

Step 3: Email Configuration

Here, we need to add email configuration in .env file. We are sending email after user registration so we need to add email smtp details for send email.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=yourpass
MAIL_ENCRYPTION=tls

Step 4: Create Auth

Laravel provide very quick way to create registration, login and forgot password with routes by auth command, So simply run bellow command to create:

php artisan make:auth

Step 5: Email Verification Setup

In last step, we need to add email verification setup, so basically we have to add email verification class implement in user model, use middleware for protection. So just update like as bellow files one by one:

app/User.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',
    ];
}

Routes/web.php

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

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class HomeController extends Controller
{

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

    public function index()
    {
        return view('home');
    }
}

That’s it. Hope it will work for you. you can read also

Codechief is a very fast growing community among programmers and have a reach of around 1 million+ readers globally. Contribution at Codechief is open for all those who have a passion to learn and help others by sharing their knowledge. If you think you have the zeal to learn, start contributing on Codechief contribute. you can also mail your article to [email protected] See your article appearing on the codechief main page and help other code.

We believe that everyone has the right to learn, so we allow both students and professionals to contribute on Codechief.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website