1
votes

i'm reading in Authentication section in laravel website https://laravel.com/docs/5.2/authentication

can anyone explain how I can do this , like the documentation explains , to specify separate tables for authentication ... i will quotes from laravel like below :

Accessing Specific Guard Instances

You may specify which guard instance you would like to utilize using the guard method on the Auth facade. This allows you to manage authentication for separate parts of your application using entirely separate authenticatable models or user tables.

The guard name passed to the guard method should correspond to one of the guards configured in your auth.php configuration file:

if (Auth::guard('admin')->attempt($credentials)) {
    //
}
2

2 Answers

3
votes

You kinda have to read the examples of adding custom guards and providers, the configuration part of it mainly. You can use the same auth 'driver', you just want to adjust what model is used by the Auth user provider.

config/auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    // add another one
    // use the same driver, 'session', but a different user provider
    'admin' => [
         'driver' => 'session',
         'provider' => 'admins',
     ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    // add a provider using Eloquent but using a different model
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
]

Then you should be able to specify the guard admin to Auth. As long as that Admin model implements Authenticatable and you are passing the appropriate credentials to attempt on Auth you should be good.

0
votes

If you have more middlewares and you want to apply more than one guard and by this i mean middleware you can do this in any Middleware.php file:

public function handle($request, Closure $next)
{
    // if a user is Agency or Admin, let him through
    if (Auth::user()->isAgency() || Auth::user()->isAdmin()) {
        return $next($request);
    } 
    // else show error page
    abort(403);
}