Enter your keyword

Laravel Framework: Authentication – Email Verification with Activation Code

Laravel Framework: Authentication – Email Verification with Activation Code

Laravel Framework: Authentication – Email Verification with Activation Code

In the previous tutorial, were able to setup basic user authentication through the login and register options. But in a live production environment, you would want to verify that the users actually own and have access to the email addresses they have provided for the system.

This functionality unfortunately doesn’t come bundled in Laravel just like authentication was, but its still easy to implement through existing packages like Jean Ragouin‘s Laravel User Verification package. However, for this tutorial am going to implement the user account email verification functionality from scratch since I found out that its not so generic to be perfectly encapsulated in a package.

#1. Users Table

We are going to add two new columns (verified, and email_token) to the already existing Users table.

Make add_account_verification_to_users migration

Edit migration at database/migrations/...add_account_verification_to_users.php

Complete migration

Updates 'users' table

Updates ‘users’ table

#2. User Model

Now you need to modify the user model at app/user.php as follows;

You will notice that we added verified and email_token to the fillable [] array, as well as add the verified () method to the model. The new verified method will update the user field in the database upon verification.

#3. Email Verification

Create the following class app/email/EmailVerification.php

#3.1 EMAIL MESSAGE TEMPLATE

Now create the email verification template at resources/views/emails/verification.blade.php as follows;

#4. Register Controller

Will will now implement the functionality for the system to automatically send a verification link the user’s email upon successful registration.

Update the RegisterController at app/http/Controller/auth/RegisterController.php as follows;

We have overridden the register () method from the “RegistersUsers” trait so that; after successful registration, we generate a random token to use in the verification email, send the email and notice the user that a verification email has been sent to thier email account.

#4.1 Register Template

Edit the register template at resources/views/auth/register.blade.php to display a notification message to the user that a verification email has been sent to his email account.

#4.2 ACCOUNT VERIFICATION ROUTE

We also added a verify () utility method to the Register Controller verify the user’s account in the database once the user verifies the account using the link we sent him. It uses the verify method we implemented in the user model above.

Now we need to register it in the routes at routes/web.php, as its the one a user will follow to verify his/her account

#5. Login Controller

You will notice that at this stage, the user can still login straight after registration without verifying his account. This so because; we don’t check whether a user account is verified during login so lets fix that too.

Update the RegisterController at app/http/Controller/auth/RegisterController.php as follows;

We have overridden the credentials () method to required the account to be verified, therefore only verified accounts can be logged into.

#5.1 LOGIN TEMPLATE

You may also wish to notify a user that his account has been verified, during login. Update the Login.blade.php at resources/views/auth/login.blade.php as follows;