简体   繁体   English

Laravel:在api testst中撤销令牌后无法登录用户

[英]Laravel: can't login user after revoke token in api testst

After I revoke user's token with function:在我用函数撤销用户的令牌后:

public function logout(Request $r)  
{
    $r->user()->token()->revoke();

    return response()->json([
        'message' => 'Successfully logged out'
    ], 200);
}

I can't login with:我无法登录:

$user=User::where('email',$r->email)->first();
    if($user)
    {
            $credentials = $r->only('email', 'password');

            if(!Auth::attempt($credentials)){
                return response()->json(['message' => 'Unauthorized'], 401);
            }

            $user = $r->user();

            $tokenResult = $user->createToken('Personal Access Token');
            $token = $tokenResult->token;

            $token->save();

            $data = [
                'access_token' => $tokenResult->accessToken,
                'token_type' => 'Bearer',
                'expires_at' => Carbon::parse(
                        $tokenResult->token->expires_at
                )->toDateTimeString()
            ];



        return response()->json(['data'=>$data,'message'=>'Successfully logged','status'=>'success'], 200);

Because I get 'Method Illuminate\\Auth\\RequestGuard::attempt does not exist.'因为我得到 'Method Illuminate\\Auth\\RequestGuard::attempt 不存在。 in response作为回应

It is something to do with how you called Auth .这与您如何调用Auth Check how you imported it (if you have) bellow your namespace.检查你是如何在你的命名空间下导入它的(如果你有的话)。 You can use use \\Auth;您可以use \\Auth; or just not import it and use a slash before it and it should work fine.或者只是不导入它并在它之前使用斜杠,它应该可以正常工作。

On tinker I can call Auth::attempt() and \\Auth::attempt() without any errors.在 tinker 上,我可以毫无错误地调用Auth::attempt()\\Auth::attempt()

So, for a quick fix, try putting a \\ before Auth .因此,为了快速修复,请尝试在Auth之前放置一个\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM