php - 为什么当用户新注册和已经登录时,注销(护照)的行为会有所不同?
问题描述
在用户新注册并被重定向到主页时 - 他们可以通过该logout()
方法毫无问题地注销。
但是,当用户使用现有凭据登录然后尝试注销时 - 我收到一条错误消息,message: "Call to a member function token() on null"
即使令牌不是null
.
我不确定自己做错了什么,我尝试四处寻找可能的解决方案,但无济于事。
public function login(Request $request) {
$credentials = ['email' => $request->email, 'password' => $request->password];
if (Auth::guard('web')->attempt($credentials, false)) {
$success = auth('web')->user()->createToken('MyApp')->accessToken;
return response()->json([
'message' => 'Successfully logged out',
'loginToken' => $success
]);
}
return $this->sendError('Unauthorized.', ['error' => 'Unauthorized']);
}
public function logout(Request $request) {
// Successfully revokes token when the user logs out if newly registered
// However, it doesn't revoke token (logout) if the user's logged in and tries to logout
$request->user()->token()->revoke();
return response()->json([
'message' => 'Successfully logged out'
]);
}
解决方案
推荐阅读
- makefile - 使通配符功能截断/删除输入的文件名
- docker - SSH_AUTH_SOCK 在 Jenkins 工作容器内运行的 docker 容器中不起作用(Docker 中的 Docker)
- django - Get reverse relation in Django Serializer
- mysql - mysqld:表 'mysql.plugin' 不存在 [错误] 无法打开 mysql.plugin 表。请运行 mysql_upgrade 来创建它。- Mac BigSur 操作系统
- dolphindb - 只能显示一个外键
- android - 如何确定用户是否从 google play store 订阅中重新订阅过期订阅
- r - R降价,控制台输出为内联并在预览选项卡中绘图
- angular - 从链接 Angular 12 中检索值
- javascript - 如何修复子字符串在 vue.js 3 中不起作用?
- php - 强制重定向登录用户