Laravel Laravel 7

Laravel 7 Email Verification Tutorial Example

Pinterest LinkedIn Tumblr

Hey, Real Programmer, I am going to share with you How to send email verification example in laravel 7, I would like to share with you how to verify email after user registration in the laravel app. I will use laravel new feature MustEmailVerify Contracts and after the user successfully verifies email that time we will authenticate and redirect to the user dashboard. We will show you each thing step by step.

If the user registers with us but does not verification of email in laravel project. User can not access the dashboard in laravel based project. The user can show the message “Before proceeding, please check your email for the verification link. If you have not received the email, then click to request another.”

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

Laravel 7 New Email Verification

  1. Install Laravel Setup
  2. Setup Database and SMTP Details
  3. Create Auth Scaffolding
  4. Migrate Database
  5. Add Route
  6. Add Middleware In Controller
  7. Run Development Server

Step 1: Download laravel 7

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

Go inside of laravel7EmailVerification folder

cd laravel7EmailVerification

Step 2: Setup Database and SMTP Details inside of .env file

Database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelEmail
DB_USERNAME=root
[email protected]

Email


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

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

php artisan migrate

Step 3: Create Auth Scaffolding

You have to follow few steps to make auth in your laravel 7 application.
First, you need to install the laravel/UI package as like bellow:

composer require laravel/ui

Now you can use a quick way to create registration, login and forgot password with routes by auth command, So simply run bellow command to create:

php artisan ui bootstrap --auth

Install NPM:

npm install

Run NPM:

npm run dev

Step 4. Migrate Database

php artisan migrate

Step 5: Add Route

Add Routes inside of routes/web.php

Route::get('/', function () {
    return view('welcome');
});
  
Auth::routes(['verify' => true]);
  
Route::get('/home', '[email protected]')->name('home');

Step 6: Adding Middleware

Inside of home controller

Example: $this->middleware(['auth','verified']);
<?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\Http\Response
     */
    public function index()
    {
        return view('home');
    }
}

Model Step inside of app/User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

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

Step 7 : Run Server

php artisan serve

Write A Comment

Open chat
Hey Visitor,

How can I help you?