Wednesday, March 15, 2017

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

I have a customized LoginController with two functions:

  • loginCustomer that runs Auth::guard('customer')->attempt(...);
  • loginEmployee that runs Auth::guard('employee')->attempt(...);

I have customized two guards in config.auth that points to my two Models (Customer and Employee) and protect the routes of backoffice and frontend.

Now in my customized LogoutController i want to run Auth::logout() but it doesn't work because i think it uses the default guard.

It only works if i specify Auth::guard('customer')->logout() or Auth::guard('employee')->logout(), depending the guard that was used to login.

Is there any way to get the guard used to authenticate the user so i can use only Auth::guard($guard)->logout?



via Ricardo Carvalho

Advertisement