注销时无法检索用户 laravel

问题描述 投票:0回答:1

这是我的

logout
方法在
AuthController

public function logout(Request $request): Response
{
    $userId = $request->user()->id;
    $token = $request->bearerToken();

    $this->_authService->logout($userId, $token);

    return response('Logged out.', 201);
}

我正在从请求中精细地检索用户 ID,因为他已通过身份验证。

public function logout(int $userId, string $token)
{
    $userSessionId = $this->_userSessionService->getUserSessionByToken($userId, $token)->id;

    Auth::logout();
    $this->_userSessionService->deleteUserSession($userSessionId);
}

然后注销用户并删除我的

AuthService
中的用户会话。

从控制器中的用户模型中检索它时得到

"message": "Attempt to read property \"id\" on null",
当我尝试在控制器中输入
dd()
用户 ID 时,它会照常记录并且不为空。但据我了解,
Auth:logout()
在到达控制器之前以某种方式执行,并且用户实际上为空。 我怎样才能避免这种情况并确保
Auth::logout()
在到达服务时执行。

更新:

UserSessionService

public function getUserSessionByToken(int $userId, string $token): ?UserSession
{
    return $this->_userSessionRepository->getUserSessionByToken($userId, $token);
}

public function deleteUserSession(int $userSessionId)
{
    return $this->_userSessionRepository->deleteUserSession($userSessionId);
}

更新:

UserSessionRepository

public function getUserSessionByToken(int $userId, string $token): ?UserSession
{
    return $this->_userSession
        ->where('token', $token)
        ->where('user_id', $userId)
        ->first();
}

public function deleteUserSession(int $userSessionId): Response
{
    $this->_userSession->find($userSessionId)->delete();

    return response([
        "message" => "User Session deleted successfully"
    ], 201);
}
php laravel jwt logout
1个回答
0
投票

没关系,我只是在这个问题出现之前编写了一些 ResponseFormatter 中间件,它导致了问题,但回溯由于某种原因指向控制器......

© www.soinside.com 2019 - 2024. All rights reserved.