Laravel - 如何获得对用户进行身份验证的警卫?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42809403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:34:00  来源:igfitidea点击:

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

laravel

提问by Ricardo Carvalho

I have a customized LoginController with two functions:

我有一个自定义的 LoginController 有两个功能:

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

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

loginCustomer 运行 Auth::guard('customer')->attempt(...);

loginEmployee 运行 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.

我在 config.auth 中自定义了两个守卫,指向我的两个模型(客户和员工)并保护后台和前端的路由。

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

现在在我自定义的 LogoutController 中,我想运行 Auth::logout() 但它不起作用,因为我认为它使用默认保护。

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

它仅在我指定Auth::guard('customer')->logout()或时才有效Auth::guard('employee')->logout(),具体取决于用于登录的守卫。

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

有什么方法可以让警卫用来验证用户的身份,以便我只能使用Auth::guard($guard)->logout

回答by Alexander Reznikov

You can use shouldUsemethod:

您可以使用shouldUse方法:

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

调用此方法后,您可以通过之前通过shouldUse方法设置的守卫注销用户。

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:

在此之后,您可以使用 Auth::logout 并且shouldUse将使用先前选择的守卫(via ):

// 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

关于此方法的简短文档:https: //laravel.com/api/5.4/Illuminate/Auth/AuthManager.html#method_shouldUse

回答by Tomasz Rup

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();
  }