Reputation: 723
I am using laravel 5.2 and I am trying to log in with more than one auth guard but only the default is working.
This is my code.
Guards
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins'
]
],
Providers
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
Authenticate Middleware
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
Routes
Route::get('/foo', function(){
if (Auth::guard('web')->attempt(['email' => '[email protected]', 'password' => 'gareth'])){
return redirect('/');
}else{
return 'No!';
}
});
Route::get('/bar', function(){
if (Auth::guard('admin')->attempt(['email' => '[email protected]', 'password' => 'password'])){
return redirect('/');
}else{
return 'No!';
}
});
Both routes return true when the attempt method is called but only the web guard actually logs a user in but if i switch the default guard to admin, the admin guard works and the web guard doesn't.
Can someone help me in solving this?
Upvotes: 2
Views: 336
Reputation: 723
I found a github repo that solved my problem. https://github.com/gregoryduckworth/laravel-multi-auth
Upvotes: 0