Multiple authentications using AuthController in Laravel 5.2

Multiple authentications are very important in the big application of laravel.  If you work on a big project then you frequently prefer to various tables, like we always prefer “users” table for an end user registration and “admins” table for admin user that means secure strong protection. We forever use Auth for performing user authentication but you have searched how to make admins with auth. The following steps are used to done this requirement.

Step 1: Open the composer and point to project directory. Run the following command for creating a new laravel project, if you already setup the project just avoid this step. Also update .env file for Database connection.

Step 2: Now we need to get default authentication controller for users. In laravel, default predefined User model, User table migration and their own authentication controller are there. We can enable that with the help of following commands.

Step 3: Now we are going to create Admin model for Admin users. This will be done by the following command

Now open up Admin model file, In that you will see that it extends default Model class. In sequence to perform authentication with Admin model we want to replace Model class with Authenticatable class. Also add the required variables such as fillable and hidden.

To be change like below,

Step 4: Now we are going to create migration class for Admin table. This will be done by the following command.

Open the Admin migration and add the required field like user table else use following code,

Step 5: Now the migrate command to create admins table in DB. Following command use to do this,

Step 6: Setup Gaurd in auth.php for admis, this file contain guards, providers, and passwords. Just add the one role for admin privilege.

Step 7: Now we need to define Admin authentication controller. It’s very easy to do this. Just copy the auth folder from app/Http/Controllers path and paste it same place and rename it as AdminAuth.

(Make sure that you’ve changed the namespace in AuthController and PasswordController files).

Then little bit work is there like redirection path for login and logout, guard name and two functions for showing the login and registration view for admin. For this add the following code in app/Http/Controllers/AdminAuth/AuthController.php

Step 8: Now we need to define Admin login and registration page. It’s also very easy to do this. Just copy the auth folder from resources/views/auth path and paste it same place and rename it as admin.

(Make sure that you want to create the layout for admin view and extend with login and register blade files in admin folder).

Then little bit work is there like redirection path for Home, Login and Register links in that admin layout and form post URL also. it seems like,

{{ url(‘/login’) }} => {{ url(‘admin/login’) }}
{{ url(‘/register’) }} => {{ url(‘admin/register’) }}

Here by default Auth::->user() will get users authentication only. If we want to chage it for admin use gaurd name in this fuction like Auth::guard(‘admins’)->user() and Auth::guard(‘admins’)->user()->name;

Step 9: Now we going to create Controller for Admin user, run the following command to create an AdminController class.

In this file, just define index function show the admin dashboard page if the admin user gets logged in.

Step 10: One important thing we missed to add for the admin user, that is middleware function. Middleware function is use to restrict normal end users to access the particular pages in our application. It helps to hide some information from end users. Following command use to create middleware for restricting not admin users,

in RedirectIfNotAdmin middleware file just add the guard name and redirection path,

Register our middleware in kernel.php

Add the middleware in AdminController file using __construct() method. AdminController file look like,

Finally, add the routes for admin users, by default users routes are added in routes. following code for admin route in routes.php file

Now we test the multi-auth function using bellow URL address,
For Users,
example.com/login
example.com/register

For Admins,
example.com/admin/login
example.com/admin/register

 

Learn Infinity

Learn Infinity is the most famous Programming & Web Development blog. Our principal is to provide the best online tutorial on web development. We execute the best tutorials for web experts this will help developers, programmers, freelancers and proving free resource you can download or preview the tutorials.

Leave a Reply

Your email address will not be published.

Back to top