Laravel - How to get the guard that authenticated the user? Laravel - How to get the guard that authenticated the user? laravel laravel

Laravel - How to get the guard that authenticated the user?


You can use shouldUse method:

After the call of this method you can logout user via guard you was previously set by shouldUse method.

In your case:

if( Auth::guard('customer')->attempt(...) ){    Auth::shouldUse('customer');}if( Auth::guard('employee')->attempt(...) ){    Auth::shouldUse('employee');}

After this you can use Auth::logout and previously choosen guard (via shouldUse) will be used:

// just use Auth::logout without Auth::guard(GUARDNAME)->logout()Auth::logout();

Short documentation about this method: https://laravel.com/api/5.4/Illuminate/Auth/AuthManager.html#method_shouldUse


This might not be the perfect solution, but it works. Basically, just go through all the guards and check if the user is authenticated by that guard. If he is - log him out. Be aware that this will log him out of all the guards he is logged in to.

This code would go to your logout controller:

  $guards = array_keys(config('auth.guards'));  foreach ($guards as $guard) {    if(Auth::guard($guard)->check()) Auth::guard($guard)->logout();  }