Skip to content

Email Verification

Brian Matovu edited this page Mar 24, 2020 · 6 revisions

This wiki is base off Laravel 6.0 documentation on email verification

Introduction

Many web applications require users to verify their email addresses before using the application. Rather than forcing you to re-implement this on each application, Laravel provides convenient methods for sending and verifying email verification requests.

And we've ported the same function to our multi authentication package

In this wiki; we shall demonstrate this using the default guard "admin" but its not limited to this particular guard.

Model Preparation

To get started, verify that your App\Admin model implements the Illuminate\Contracts\Auth\MustVerifyEmail contract:

<?php

namespace App;

use App\Notifications\Admin\Auth\ResetPassword;
use App\Notifications\Admin\Auth\VerifyEmail;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    // ...
}

Database Considerations

The Email Verification Column

Next, your admin table must contain an email_verified_at column to store the date and time that the email address was verified. By default, the admins table migration included with this package already includes this column. So, all you need to do is run your database migrations:

php artisan migrate

Routing

This package includes the Admin\Auth\VerificationController class that contains the necessary logic to send verification links and verify emails. To use the necessary routes for this controller, just un-comment them in routes/admin.php:

Route::group(['namespace' => 'Admin'], function() {
    // ...
    Route::post('email/resend', 'Auth\VerificationController@resend')->name('admin.verification.resend');
    Route::get('email/verify', 'Auth\VerificationController@show')->name('admin.verification.notice');
    Route::get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('admin.verification.verify');
});

Protecting Routes

Route middleware can be used to only allow verified admins to access a given route. This package ships with the admin.verified middleware, which is defined at App\Auth\Middleware\EnsureAdminEmailIsVerified.

Register this middleware in your application's HTTP kernel.

protected $routeMiddleware = [
    // ...
    'admin.verified' => \App\Http\Middleware\EnsureAdminEmailIsVerified::class,
],

You will also need to do is attach the middleware to a route definition:

Route::get('/', 'HomeController@index')->middleware('admin.verified')->name('admin.dashboard');

Views

This package will generate all of the necessary email verification views when the multi-auth:install command is executed. This view is placed in resources/views/admin/auth/verify.blade.php. You are free to customize this view as needed for your application.

After Verifying Emails

After an email address is verified, the admin will automatically be redirected to /admin. You can customize the post verification redirect location by defining a redirectTo method or property on the VerificationController:

protected $redirectTo = '/admin';