如何在Laravel中撤销多重身份验证中的令牌?

时间:2018-10-16 08:14:26

标签: php laravel revoke

编写登录方法并成功。我的身份验证配置:

   'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'admin-web' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'admin-api' => [
            'driver' => 'passport',
            'provider' => 'admins',
        ],
    ],

写出注销方法,例如:

 public function logout(Request $request)
    {
        Auth::guard('admin-web')->logout();
        $value = $request->bearerToken();
        $id = (new Parser())->parse($value)->getHeader('jti');
        $token = $request->user()->tokens->find($id);
    }

在此列中找不到令牌:

    $token = $request->user()->tokens->find($id);

和错误:

Trying to get property 'tokens' of non-object

有什么解决办法吗?

2 个答案:

答案 0 :(得分:0)

一旦您致电Auth::guard('admin-web')->logout(),您将无法从请求中检索用户。 您必须在注销用户之前检索令牌:

public function logout(Request $request)
{
      $value = $request->bearerToken();
      $id = (new Parser())->parse($value)->getHeader('jti');
      $token = $request->user('admin-web')->tokens->find($id);
      Auth::guard('admin-web')->logout();
}

答案 1 :(得分:0)

使用如下,然后再检查一次。您错过了$ id参数。

 public function logout(Request $request,$id)
  {
  $value = $request->bearerToken();
  $id = (new Parser())->parse($value)->getHeader('jti');
  $token = $request->user('admin-web')->tokens->find($id);
  \Auth::guard('admin-web')->logout();
  }