簡體   English   中英

Laravel:在api testst中撤銷令牌后無法登錄用戶

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

在我用函數撤銷用戶的令牌后:

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

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

我無法登錄:

$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);

因為我得到 'Method Illuminate\\Auth\\RequestGuard::attempt 不存在。 作為回應

這與您如何調用Auth 檢查你是如何在你的命名空間下導入它的(如果你有的話)。 您可以use \\Auth; 或者只是不導入它並在它之前使用斜杠,它應該可以正常工作。

在 tinker 上,我可以毫無錯誤地調用Auth::attempt()\\Auth::attempt()

因此,為了快速修復,請嘗試在Auth之前放置一個\\

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM