6

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

2 Answers 2

24

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

    Route::group(['middleware' => ['auth:comsumer, member'] ], function(){
        Route::get('/home', 'HomeController@index');
    });
Sign up to request clarification or add additional context in comments.

2 Comments

remember to avoid whitespaces: 'auth:comsumer,member']
but what if I need to give the member ->only('show')?
2

While defining the route group do something like this,

Route::group(['middleware' => 'auth:guard_name'], function () {
    //Your Route goes here
}

Hope that solves your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.