Laravel 7/6 Email Verification Tutorial Example

Laravel 7/6 Email Verification Tutorial Example

In this simple laravel 7/6 email verification example, we would like to share with you how to verify email after user registration in the laravel app. We 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.”

Laravel default email verification process is very simple and easy. We can also modify the default email template and create a new custom email template of email verification, password reset, forgot password and replace to default templates. Laravel email verification feature has reduced the most time of development.

Laravel 6 New Email Verification

  • Install Laravel Fresh Setup
  • Setup Database and SMTP Detail
  • Create Auth Scaffolding
  • Migrate Database
  • Add Route
  • Add Middleware In Controller
  • Run Development Server
  • Conclusion

1. Install Laravel Fresh Setup

First, we need to download the laravel fresh setup. Use the below command and download fresh new laravel setup :

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

2. Setup Database

After successfully download laravel 6 Application, Go to your project .env file and set up database credential and move next step :

 DB_CONNECTION=mysql 
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

After successfully set database credentials, Now setup SMTP credential in .env. We have used a mail trap SMTP credential in this example.

 MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_user_name
MAIL_PASSWORD=your_mailtrap_user_password
MAIL_ENCRYPTION=tls

The SMTP service you want can be used for email verification in the Laravel project. Like Gmail SMTP, Yahoo SMTP, Melgun SMTP, etc

3. Create Auth Scaffolding

This command will create routes, controllers and views files for Laravel Login Authentication and registration. It means to provide a basic laravel login authentication and registration Complete system. Let’s open the command prompt and type the below command.

Next step, we need to create new blade views for login, registration, forget the password and reset password. So visit this link for new laravel auth scaffolding.

4. Migrate Database

Before we run php artisan migrate command go to app/providers/AppServiceProvider.php and put the below code :


use Illuminate\Support\Facades\Schema;

function boot()
{
Schema::defaultStringLength(191);
}

Now we will run the below command. It will create some tables in our database, Use the below command :

php artisan migrate

After migrating the database tables we need to add the MustVerifyEmail Contracts in App/User.php. Open the App/User.php file add the constructor like :

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

    /**
     * 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',
    ];
} 

5. Add Route

We will create one route in the web.php file for email verification in the laravel 6 App. Go to app/routes/web.php file and create one below routes here :


Route::get('/', function () {
return view('welcome');
});

//first
Auth::routes();

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

6. Add Middleware In Controller

We need to add Middleware in the controller constructor. Go to app/controllers/HomeController
and put this line $this->middleware([‘auth’, ‘verified’]); inside of constructor :

<?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');
    }
}

when a user tries to access the dashboard our laravel 6 app without email verification. Users can not access without verifying email in laravel app, We have added the line of code inside the controller constructor. It will check the user’s email verification whether an email is verification or not in the laravel project.

7. Run Development Server

We need to start the development server. Use the PHP artisan serve command and start your server :

 php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080

Now we are ready to run our example so run bellow command to quick run.

 http://localhost:8000/

Conclusion

In this laravel 6 tutorial for email verification, We have successfully verified email address after user registration. If the user does not verify his/her email. Users can not log in with our laravel 6 Based Project. our examples run quickly.

laravel 6 tutorial for email verification example looks like this :

laravel 6 Based Project – Registration Display

laravel 5.7 email verification

After registration, we got the email for verification (laravel project). Check the email and verify the email address.

Email Verification – After Login Display, If the user does not verify it’s email :

laravel 5.7 email verification

laravel 6 Based Project – Successfully Verify email address :

laravel 5.7 email verification

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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