Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : Multi Guard in Auth Middleware

Tags:

php

laravel

I have a Laravel Application using Multi User tables for different roles.

I have added 2 custom guards like this:

'guards' => [
    'consumer' => [
        'driver' => 'session',
        'provider' => 'consumer',
    ],
    'member' => [
        'driver' => 'session',
        'provider' => 'member',
    ]
]

And I want to share the the same route with both of consumer and member. But I dont know how Laravel pass guard name to the Auth middleware.

Look at file Illuminate\Auth\Middleware\Authenticate

protected function authenticate(array $guards)
{
    if (empty($guards)) {
        return $this->auth->authenticate();
    }

    foreach ($guards as $guard) {
        if ($this->auth->guard($guard)->check()) {
            return $this->auth->shouldUse($guard);
        }
    }

    throw new AuthenticationException('Unauthenticated.', $guards);
}

If I can pass 2 custom guards to the $guards variable, It can share the route between custom and user. But I dont know how to pass the guard's name

like image 301
anhlt Avatar asked Nov 29 '22 22:11

anhlt


1 Answers

you can just pass the guards with comma, for exapmle:

    Route::group(['middleware' => ['auth:comsumer, member'] ], function(){
        Route::get('/home', 'HomeController@index');
    });
like image 170
lamoimage Avatar answered Dec 05 '22 23:12

lamoimage