[英]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.