Create Muti-Auth in Laravel 5.2

When creating any web application, there is seldom a requirement where you only need one level of authentication. Or to be more exact where you have only one type of users. Normally we work win environment where we require an administrative user and another public user. Thus the two users may have different types of data and tables. In such we will also require two different types login processes for each user types. By default laravel ships with User auth model. So how doe we add another authentication model to the system.

Following 6 simple steps will guide us towards creating a multi-auth system where we can create different authentication for different model/tables.

Step 1:  Preparing the views

So first lets start by creating views for our login and register page. The design can be whatever you want and however you wish. We just need to make sure that the names of input fields in the form for your design is satisfied by the laravel authentication model.

For that, we need to follow some conventions used by the laravel.

So for the login form the default names for the input fields are email & password.

and for the register form the default names are name, email, password & password_confirmation.

Just following this convention in your forms will complete the views for the system.

Step 2: Prepare Model

Now we need to prepare the model for the new authentication object. Execute the following from the termimal

$ php artisan make:model Admin -m

This will make  a new model Admin.php in app folder and also create a migration file with create_admins_table in the database/migrations folder.

Open up the Admin.php and make the class similar to User.php in all manners except the class name Admin. Also open the create_admins_table migration file and define the table definitions same as that in create_users_table. Then run the migration.

$ php artisan migrate

Step 3 : Create Guards

We need to make some changes in config/app.php to make laravel know that we will be using a different guard for our admin authentication. So open up the file and go to ‘guards’ section. Create a new guard ‘admin‘ similar to ‘web‘ guard and change the providers to ‘admins‘.

Now we will create a new provider that we have defined in the guard. In the ‘providers‘ section create a new provider ‘admins‘ similar to ‘users’ and change the model to App\Admin::class, thus pointing the provider to the new created Admin model.

This completes the creation of guard for our authentication process.

Step 4 : Create Controller

Then lets make controllers to connect the views and the model.

$ php artisan make:controller Admin\AuthController

This creates AuthController in side controllers\Admin folder.

Make controller\Admin\AuthController similar to controller\AuthController in all manners. Just make sure that the Admin\AuthController is using App\Admin in the use section.

In the controller after the first line, which is usually ‘use Authenticates. . . . .’  and before the start of the constructor add the following lines of code.

protected $guard = 'admin';
//login and register form that we created earlier
protected $loginView = 'admin.auth.login'; 
protected $registerView = 'admin.auth.register';

Then inside the validator() and create() method replace any instance and declaration of User or users with Admin and admins.

This completes our controller for the authentications.

Step 5 : Create middleware

Lets create a middleware to safeguard our admin routes and prevent guest access.

$ php artisan make:middleware AdminAuthentication

So inside the middleware class, make the required changes as below

 

function handle($request, Closure $next, $guard = 'admin'){
    if(Auth::guard($guard)->guest()){
        return redirect()->guest('/admin/login');
    } 

    return $next($request);
}

This will redirect any un-authenticated guest to the admin login url.

Remember to register the middleware in the kernel.php, in our case registered as ‘auth.admin

Step 6 : Create routes

Finally we need to create the route for all to work. Create routes following the matching controllers.

Route::get('admin/register','Admin\[email protected]');

Route::post('admin/register','Admin\[email protected]');

Route::get('admin/login','Admin\[email protected]');

Route::post('admin/login','Admin\[email protected]');

Route::group(['prefix'=>'admin','middleware'=>'auth.admin'],function(){

	Route::get('/home',function(){
		return "Welcome! ".Auth::guard('admin')->user()->name;
	});


	Route::get('/logout','Admin\[email protected]');

});

Finally run the project and register the user and then try out all the routes that have been defined.

If everything is fine we have successfully created laravel multi-auth system.

Reference Video :

Leave a Reply

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