Laravel-如何获得对用户进行身份验证的防护? [英] Laravel - How to get the guard that authenticated the user?

查看:66
本文介绍了Laravel-如何获得对用户进行身份验证的防护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a customized LoginController with two functions:

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

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

运行Auth :: guard('employee')-> attempt(...);的登录员工

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

我在config.auth中自定义了两个防护,它们指向我的两个模型(Customer和Employee),并保护后台和前端的路由.

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.

现在在我自定义的LogoutController中,我想运行Auth :: logout(),但是它不起作用,因为我认为它使用了默认的防护措施.

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

仅当我指定Auth::guard('customer')->logout()Auth::guard('employee')->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($guard)->logout吗?

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

推荐答案

这可能不是完美的解决方案,但它可以工作.基本上,只需遍历所有防护措施,然后检查该防护措施是否验证了用户的身份.如果他是-请注销他.请注意,这将从他登录的所有防护中注销.

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

这篇关于Laravel-如何获得对用户进行身份验证的防护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆