Edvinas Kručas has created a nice package to extends default Laravel 5.2 authentication with user verification by email.

Laravel comes with a great authentication and authorization system. As of 5.2 version, it also provides authentication scaffolding for views, routes, and controllers. But yet one feature is missing there – user email verification. Recently I have been working on a package to provide verification functionality with a hassle-free routine, a package can be found on GitHub and Packagist.

Check out this package on GitHub.

What does this package do?

This package extends default Laravel 5.2 authentication with user verification by email. Below are shown basic flows for login, registration, verification.

enter image description here

User email verification registration flow

enter image description here

User login flow

enter image description here

User email verification flow

Prerequisites

You need standard Laravel 5.2 auth to be installed. How? You can check it on Laravel documentation.

Laravel user email verification package usage

Require this package in your composer.json by typing in your console

composer require edvinaskrucas/laravel-user-email-verification

Register new service provider in your config/app.php file

'providers' => [
    // ...

    Krucas\LaravelUserEmailVerification\UserEmailVerificationServiceProvider::class,
],

Publish package specific configuration, views, and translations

php artisan vendor:publish --provider="Krucas\LaravelUserEmailVerification\UserEmailVerificationServiceProvider"

Once you have published user email verification package resources you have to run command described below, this will create new controller file located at app/Http/Controllers/Auth/VerifyController.php which will handle all routine related to email verification. Additionally, it will create default migrations and adjust routes file. Migrations will add new columns to default users table, and table to store verification tokens.

php artisan verification:make

We are almost done! :) Now you have to edit default AuthController located at app/Http/Controllers/Auth/AuthController.php, in this file we are going to add verification handling for login and registration actions.

Registration – this will add a hook into a controller to send a verification email to a newly created user, and skip login action if user MUST (verify – check this option in a readme file of a package) verify his account before login.

Login – do not let user login before his account is verified, this is valid just if you choose that user MUST verify his account first.

And this is how your controller should look like

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Krucas\LaravelUserEmailVerification\AuthenticatesAndRegistersUsers as

VerificationAuthenticatesAndRegistersUsers; use Validator; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins, VerificationAuthenticatesAndRegistersUsers {
        AuthenticatesAndRegistersUsers::redirectPath insteadof VerificationAuthenticatesAndRegistersUsers;
        AuthenticatesAndRegistersUsers::getGuard insteadof VerificationAuthenticatesAndRegistersUsers;
        VerificationAuthenticatesAndRegistersUsers::register insteadof AuthenticatesAndRegistersUsers;
    }

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Krucas\LaravelUserEmailVerification\AuthenticatesAndRegistersUsers as

VerificationAuthenticatesAndRegistersUsers; use Validator; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins, VerificationAuthenticatesAndRegistersUsers {
        AuthenticatesAndRegistersUsers::redirectPath insteadof VerificationAuthenticatesAndRegistersUsers;
        AuthenticatesAndRegistersUsers::getGuard insteadof VerificationAuthenticatesAndRegistersUsers;
        VerificationAuthenticatesAndRegistersUsers::register insteadof AuthenticatesAndRegistersUsers;
    }

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

After controller update, it’s time for User model. User model class must implement RequiresEmailVerificationContract, for new method implementation, you can use predefined trait RequiresEmailVerification, or add your own method implementation.

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Krucas\LaravelUserEmailVerification\Contracts\RequiresEmailVerification

as RequiresEmailVerificationContract; use Krucas\LaravelUserEmailVerification\RequiresEmailVerification;

class User extends Authenticatable implements RequiresEmailVerificationContract
{
    use RequiresEmailVerification;

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

Now you are all set for a default usage of a user email verification package, just hit the browser to http://yourpage/verify you will see page looking like that