User email verification for Laravel 5

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.

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.

 

User email verification registration flow
User registration flow
User email verification login flow
User login flow
User email verification flow
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

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

Publish package specific configuration, views, and translations

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.

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

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.

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

User email verification page
User email verification page

Check out this package on GitHub.

30 thoughts on “User email verification for Laravel 5”

  1. Trying to implement your package I come accross with the following error:

    UnexpectedValueException in VerificationBroker.php line 155:
    User must implement RequiresEmailVerification interface.

    Moreover I changed:

    $this->middleware($this->guestMiddleware(), [‘except’ => ‘logout’]);

    to:

    $this->middleware(‘guest’, [‘except’ => ‘logout’]);

    Any thoughts?

  2. Now I get this:

    Swift_TransportException in AbstractSmtpTransport.php line 383:

    I have set up mailgun for mail sendings. Is it relevant to the problem? It works fine for password reset though.

  3. php artisan vendor:publish –provider=”Krucas\LaravelUserEmailVerification\UserEmailVerificationServiceProvider”

    I get “Nothing to publish for tag [].

    1. OK I see on Packagist it’s actually

      php artisan vendor:publish –provider=”Krucas\LaravelUserEmailVerification\UserEmailVerificationServiceProvider” –tag=“config”

      When you enter “php artisan verification:make” it fails because laravel/user/email/verification/src/Krucas/LaravelUserEmailVerification/Console/stubs/controllers/VerifyController.stub is mislabeled as verifycontroller.stub

      Now that I have this sorted out it does not seem to validate email and the user is logged right in as before… Not sure what I’m missing here.

      1. Hey,

        When you specify a tag, then it will publish just specific tag, otherwise it will publish all resources.

        And fixed that command issue, thanks for you note!

  4. I’m at this point now…

    Swift_TransportException in AbstractSmtpTransport.php line 162:Cannot send message without a sender address

    I have everything setup in my .env file so I’m not sure why it isn’t getting it. I was able to perform a test and send email via my account it’s just not working here.

    1. You have to properly set your emailing driver, that means you need to adjust config/mail.php and set “from” values.

      1. Thank you, It turns out that Laravel doesn’t like it when you put the from value in the .env file… you have to hard code it in the config/mail.php… annoying. Thank you for the great package. It works wonderfully now.

  5. hey, thanks for the tutorial, its a wonderful code,however I came acorss this matter:

    FatalErrorException in ClassLoader.php line 347: Maximum function nesting level of ‘100’ reached, aborting!

    how can I resolve it
    thanks

  6. Hello and thanks for this tutorial.
    How can I send an email right after the user is registered? I’ve implemented all the things above, but I get no email to verify the address. Thanks for your reply.

        1. This is not enough information. It will be easier to communicate via email, this way I will help you to solve this faster.

  7. When I click resend link it says!! and verification link does’ not work!
    Whoops, looks like something went wrong.

    2/2
    ErrorException in ab4ce2a82272f03435979b8e7758e5bc6da1435c.php line 26:
    Undefined variable: errors (View: E:\rangeen\resources\views\vendor\verification\auth\verification\resend.blade.php)

    1. You can adjust file and add “isset” for errors variable. But $errors should be present in each view.

  8. Here is my route!!In my VerifyController,I did’nt find
    1.showResendForm
    2. sendVerificationLinkEmail’
    3.verify
    methods
    Route::get(‘verify/resend’, [
    ‘uses’ => ‘Auth\VerifyController@showResendForm’,
    ‘as’ => ‘verification.resend’,
    ]);

    // verification token resend action
    Route::post(‘verify/resend’, [
    ‘uses’ => ‘Auth\VerifyController@sendVerificationLinkEmail’,
    ‘as’ => ‘verification.resend.post’,
    ]);

    // verification message / user verification
    Route::get(‘verify/{token?}’, [
    ‘uses’ => ‘Auth\VerifyController@verify’,
    ‘as’ => ‘verification.verify’,
    ]);

  9. can we have a method to pass a user id or an email maybe to generate and resend the verification code to the email?
    I am doing a part of my users registration manually from a mobile app without using the auth controller.

Leave a Reply

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