CodeBriefly
Tech Magazine

Email Verification Laravel 5.7 New Feature

0 1,559

Get real time updates directly on you device, subscribe now.

In this article, we will discuss “Email Verification Laravel 5.7 New Feature”. Laravel is not only a PHP framework, today its know as a new trend of PHP Web Development. Recently Laravel 5.7 is released with some of the new features, which make development more easy and flexible.

Today, we discuss one of the new feature “Email Verification”. Lots of web applications or website ask users to verify the email addresses before using the applications or website. Laravel 5.7 makes email verification easy, you don’t need to recreate all the functionality.

Create New Laravel 5.7 Setup

You can use the following command to create a new project.

composer create-project laravel/laravel lara5.7 --prefer-dist

After creation of new setup, now time to update the “.env” file. So open “.env” and update the database name, username, and password. Our basic Laravel 5.7 setup is ready for further use.

User Modal Updation

We need to verify the App/User model is using the “use Illuminate\Contracts\Auth\MustVerifyEmail” contract.

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

Database Migration

Laravel provides all the updated migration for the user table located at database/migrations. You need to verify user table contains the “email_verified_at” column. In this column date and time is stored when the user verifies there email.

Default migrations already contain this column so we can run the following command to create our authentication tables.

php artisan migrate

When we execute the migrate command, we face Syntax error or access violation 1071 error. Follow the given article and you can easily fix this issue, we already cover this in our previous article.

How to fix Laravel Syntax Error or Access Violation 1071

Creating Authentication Scaffolding

You can use the given artisan command to create the default authentication scaffolding. You can apply your customization any time on generated views. All these views located at resources/views/auth. The verify.blade.php is also included in this scaffolding.

php artisan make:auth

Routing

Controller class “Auth/VerificationController” included in the Laravel 5.7 and contains all the necessary logic to send verification link and emails. You can update the “Auth::routes()” function to register the routes for this controller.

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

Use of Middleware

Laravel 5.7 provides “verified” middleware which allows only verified user to access the protected routes. Middleware 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, is already registered in the Kernel.php. 

Route::get('dashboard', function () {
    // Only verified users may enter...
})->middleware('verified');

Handling Redirection After Verify

You can handle the redirection of the user when a user verifies their email address. Just update the “redirectTo” method or property in your “VerificationController”. By default user will automatically redirect to “/home” URL.

protected $redirectTo = '/dashboard';

Conclusion

In this article, we are discussing on the new feature email verification Laravel 5.7. Hope you like this article. You can check the official documentation here for Email Verification. Please feel free to add the comment for any query or submit your feedback 😉


You may like:

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More