Using Multiple Auth Guard in Laravel 5.3

In this post, we will create a new custom guard and use that inside the application. 

Posted by Author At January 21st, 2017

If you have a multiple user hierarchy, you may want to use multiple auth guard to automate the authentication process inside your laravel application. Although laravel default auth guard is with you to ease the development process, sometimes you just want to use multiple database table for multiple user hierarchy. In that case, laravel Authentication guard comes handy. In this post, I’m going to show you an example of custom auth guard and how to implement them in your application.

Let’s say, you have two table for users, one is for frontend users, and another is for the admin, now you wanna use different guard for these two users. In this post I suppose that you already have used the default users table for the admin users, so you already have a default laravel auth setup.

# First Step : Create a New Model for your frontend users table.

# Now, go to config/auth.php and open it up: 

This is where we will add our new authentication guard. By default, you will have an guard array like this :

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],

Now let’s add another guard, which we’re going to use for the frontend users, since we’re using the default web guard for the admin users.

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'frontend' => [
            'driver' =>'session',
            'provider' => 'frontend_users',
        ],
   ],

We added our guard named “frontend”, you can change the name as per your requirement. We now need to add a provider named 'frontend_users' in the providers array.

# Now let’s add a provider for our new authentication guard.

Initially, we will have a providers array like this :

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],

Now, we will add our another provider for our ‘frontend’ guard.

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        'frontend_users' => [
            'driver' =>'eloquent',
            'model'  => App\FrontEndUser::class,
        ],
    ],

In my example, the model name for the frontenduser table is FrontEndUser, you can replace it with your own model name that you want to use with the guard.

Now, we’re good to go, we just registered our new ‘frontend’ guard, we can go straight and use our guard in the controller. Now every auth methods are available to our new guard. Let’s see an example to login a frontend user.

<?php

namespace App\Http\Controllers\Frontend;

use Illuminate\Http\Request;
use App\FrontEndUser;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Support\Facades\Session;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

use Illuminate\Foundation\Auth\RegistersUsers;

class UserAuthController extends Controller
{
    use ThrottlesLogins;

    protected $guard = 'frontend';


public function login(Request $request)
  {
     if ($this->hasTooManyLoginAttempts($request)) {

	  $this->fireLockoutEvent($request);

          return $this->sendLockoutResponse($request);
      }

    if(Auth::guard($this->guard)->attempt([ 'email' => $request->username, 'password' =>      $request->password ]))
     {
	  $user = Auth::guard($this->guard)->user();
     }
 }
}

You can create a custom provider for your guard that has been described more in laravel authentication doc .


Posted Under