Part IV: How to Login Admins in Our Laravel 5.4 Multiple Authentication System

How to Login Admins in Our Laravel 5.4 Multiple Authentication System

 

Welcome back to this tutorial where we are creating multiple authentications in Laravel 5.4 In the previous tutorials we did set up our new admin guard, we can now reference our new admins by just using our admin guard. I think that was bulk of what is important about this tutorial. By default we have login for users, the question is, how do we login admins?

The form above only login the users, remember we have two types of admins namely user/customers and the admins. To clarify this, the above form is only going to login users using User.php model but not our Admin.php model.

In order to get login form for admins working we are going to create another controller for managing all of the logins. Before we continue, open your terminal and key in the following command:

php artisan route:list

The command above actually shows you the routes in you application, you get to see which middleware is being used by each route. Alright, now we can go ahead and create Admin login controller by typing the following command in your terminal:

php artisan make:controller Auth/AdminLoginController

After creating your AdminLoginController.php make sure it has the following code:

Now lets go through the code in AdminLoginController.php From line eleven to fifteen we are setting our middleware which is going to be used by AdminLoginController.php On line fourteen we are telling the system to use guest as our middleware but we also specify which guard to use in this case admin guard.

In our login function, from line twenty four to twenty seven, we are validating admins data against our admin model(Admin.php). In our case the rules are just email and password, This ensure that what we have in the database matches the admins input in our admin login form.

To actually login the admins, we are wrapping our method inside an if statement. On line twenty nine we are using Auth facade and specifying our admin guard which ties to Admin.php model. One of the benefits of using attempt method is that we do not need to hash our password manually when we are passing our credentials in line 29.

On line 31, Laravel keeps track of where people are trying to go, in this line we are setting our default to a route that goes to admin dashboard. On line 34, we are redirect user to admin login form with input value email and remember token if any. This only happens if they are not successfully logged-in.

On line 19, we are showing the admin login form which is located on resources/views/auth/admin-login.blade.php  Inside auth create admin-login.blade.php make sure that admin-login.blade.php has the following code:

The code above will show the admin login form, lastly we need to configure our routes. Open web.php and make sure it has the following code:

In our web route file, we are prefixing admin routes with admin and placing them inside a group. Pretty clean hah! It is always good web development practices to use named route for easy code maintainability. We have done a pretty good job so far, in our next lesson. We will be testing our admin login functionality.

To get the code visit: Laravel 5.4 Multi Authentication System

Get free email updates!
Signup now and receive an email once we publish new content.
We respect your privacy