Multiple Authentication Example in Laravel 5

To implement the multiple authentication in Laravel you can follow the following steps
1 Create model for
2 Define your guard in config\auth.php
3 Create Route
4 Create View for registration and login
5

Redirect authenticated users
Password Reset

1 Create model Admin

This will create the migration file too
You can open the migration file which has the name end with create_admins_table and edit the file with following code

Now you can run the migration command to create the admins table in the database

Then you have to add some code to Admin.php model. You can copy the code from User.php model

2 Define the Guard

I have defined guard name admin in this code
Now you need to create provider for the admin guard

I have defined the provider admins here

Now you have to tell to Admin model that I am going to use admin guard.
In Admin.php model file you can add the following line protected $guard='admin'

3 Create Route

Now I am going to create route and add middle ware to route

I have created the group route to make it easy

we have admin home page for logged admin users and we have login and logout pages too

Redirecting authenticated users

When you open the login page once you are authenticated it should redirect to some page
You can change the handle function of the in RedirectIfAuthenticated class App\Http\Middleware

Password Reset

In password reset process you have two views to deal with
1 email.blade.php
This view has the text field to enter the email address and a button to send password reset link to the given email

2 reset.blade.php
User will receive email to his inbox for password resetting. When user open the URL he can see this view to reset the password

You need to chene the action of the form to

First thing you have to do is, change the forgot my password URL
In view\admin\login.blade.php file you can change the existing code to

In view\admin you need following files copied from the resources\views\auth
login.blade.php
register.blade.php
password\email.blade.php
password\

You need to configure password broker to work with admin otherwise it will work with default users

in your config\auth.php file you need following code

Now in your AdminForgotPasswordController class you have to specify which password broker you are going to use

Reset Email Customization

You need to customize reset email otherwise it will have details related to user guard
You can add the following method to your App\Admin.php model

Now you can create the AdminResetPasswordNotification using Artisian command

AdminResetPasswordNotification.php is create inside the Notifications folder

In AdminResetPasswordNotification class, I am going to add instance variable name public $token

And changing the constructor to accept the token value

Now changing the toMail() function

Now you can test the Password Reset process

Leave a Reply

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