1.新增退出登录的接口
This commit is contained in:
@@ -115,6 +115,34 @@ class Auth extends MobileBase
|
||||
return $this->mobileSuccess($this->buildLoginPayload());
|
||||
}
|
||||
|
||||
public function userLogout(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeMobile($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$refreshToken = trim((string) $request->post('refresh_token', ''));
|
||||
if ($refreshToken === '') {
|
||||
$refreshToken = trim((string) $request->post('refreshToken', ''));
|
||||
}
|
||||
|
||||
if ($this->auth->isLogin()) {
|
||||
$userId = (int) $this->auth->id;
|
||||
if ($refreshToken === '') {
|
||||
$refreshToken = $this->auth->getRefreshToken();
|
||||
}
|
||||
$this->auth->logout();
|
||||
MobileAuthDeviceService::onUserLogout($userId);
|
||||
}
|
||||
|
||||
if ($refreshToken !== '') {
|
||||
Token::delete($refreshToken);
|
||||
}
|
||||
|
||||
return $this->mobileSuccess();
|
||||
}
|
||||
|
||||
public function tokenRefresh(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeMobile($request);
|
||||
|
||||
@@ -122,6 +122,21 @@ final class MobileAuthDeviceService
|
||||
*
|
||||
* @return string|null 失败时返回多语言 message key,成功返回 null
|
||||
*/
|
||||
/**
|
||||
* 用户主动退出:清除 Redis 中的活跃设备绑定。
|
||||
*/
|
||||
public static function onUserLogout(int $userId): void
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Redis::del(self::PREFIX_USER_DEVICE . $userId);
|
||||
} catch (Throwable) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
public static function validateUserDeviceSession(string $authToken, int $userId): ?string
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
|
||||
Reference in New Issue
Block a user