Laravel 8 Custom Email Verification Tutorial

In this article, we will see laravel 8 custom email verification. Many web applications require users to verify their email addresses before using the application. Laravel provides convenient built-in services for sending and verifying email verification requests. Email verification is the process of checking and authenticating emails that you've been given to ensure that they're authentic and that they're likely to connect you to a real person.

So, we will give you an example of custom email verification in laravel 8. For custom email verification we need some basic requirements like middleware, routes, and mail configuration.

Let's see the email verification laravel 8.

Step 1: Create Laravel 8 Application

In this step, we will create a new laravel 8 application using the below command.

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

 

 

Step 2: Configuration Database

Now, we will configure a database in the .env file.

DB_CONNECTION = mysql
DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_DATABASE = Your_database_name
DB_USERNAME = Your_database_username
DB_PASSWORD = Your_database_password

After adding database configuration, run the default migrations of laravel using the below command.

php artisan migrate

 

Step 3: Email Configuration for .env

In this step, we will set up email configuration in the .env file.

MAIL_DRIVER = smtp
MAIL_HOST = smtp.gmail.com
MAIL_PORT = 587
MAIL_USERNAME = your_email@gmail.com
MAIL_PASSWORD = your_password
MAIL_ENCRYPTION = TLS

 

 

Step 4: Install Auth

In this step, we will install the laravel/ui package for basic UI using the below command.

composer require laravel/ui

After installing laravel/ui. we will install bootstrap auth using the below command for basic login, and registration. After that, we will install NPM and run NPM in your project.

php artisan ui bootstrap --auth

Install NPM:

npm install

Run NPM:

npm run dev

 

Step 5: Setup Email Verification

Now, we will set up email verification on the user.php file. So, in the User model add an email verification class and use middleware for protection. In the User model, you need to add MustVerifyEmail Auth and implement MustVerifyEmail on the User class.

app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, 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',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

 

 

routes/web.php

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

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

 

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['auth','verified']);
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS