From c739cc5b6e1b116b7a037d3aeff3d3899daf03f9 Mon Sep 17 00:00:00 2001 From: chenhang Date: Fri, 3 Jul 2026 17:04:19 +0800 Subject: [PATCH] refactor(auth): fold guest restore into logout --- src/data/repositories/auth_repository.ts | 39 +++++++------------ .../interfaces/iauth_repository.ts | 9 +---- src/stores/auth/auth-actors.ts | 3 +- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/data/repositories/auth_repository.ts b/src/data/repositories/auth_repository.ts index bea081a3..a1e34d85 100644 --- a/src/data/repositories/auth_repository.ts +++ b/src/data/repositories/auth_repository.ts @@ -19,7 +19,13 @@ import { SendCodeRequest, } from "@/data/dto/auth"; import { User } from "@/data/dto/user"; -import { AppEnvUtil, PlatformDetector, Result, Logger } from "@/utils"; +import { + AppEnvUtil, + PlatformDetector, + Result, + Logger, + deviceIdentifier, +} from "@/utils"; import type { IAuthRepository } from "@/data/repositories/interfaces"; import { AuthStorage, type IAuthStorage } from "@/data/storage/auth"; import { UserStorage, type IUserStorage } from "@/data/storage/user"; @@ -95,35 +101,15 @@ export class AuthRepository implements IAuthRepository { }); } - /** - * 退出登录:先调 API 注销服务端会话,无论成败都清本地登录态。 - * 强制清本地的逻辑:用户即使在 API 失败时也想从设备上登出。 - */ - async logout(): Promise> { - try { - await this.api.logout(); - } catch (e) { - // API 失败也要继续清本地态 - log.warn("[AuthRepository] logout API failed, clearing local anyway", e); - } - try { - await this.storage.clearAuthData(); - await this.userStorage.clearUserData(); - return Result.ok(undefined); - } catch (e) { - // 清本地是 best-effort;理论上 LS.remove 不应失败 - return Result.err(e); - } - } - /** * 退出真实用户登录并恢复游客态。 * - * 与 `logout()` 不同,这里不会删除 guestToken / deviceId。退出后会尝试 - * 重新跑游客登录,以便恢复游客 userId;如果游客登录接口失败但本地仍有 - * guestToken,则降级回本地游客态,避免用户从聊天页被打回 splash。 + * 注意:业务上不允许从其他登录态退出到未登录态,所以这里不会删除 + * guestToken / deviceId。退出后会尝试重新跑游客登录,以便恢复游客 userId; + * 如果游客登录接口失败但本地仍有 guestToken,则降级回本地游客态,避免 + * 用户从聊天页被打回 splash。 */ - async logoutToGuest(deviceId: string): Promise> { + async logout(): Promise> { try { await this.api.logout(); } catch (e) { @@ -137,6 +123,7 @@ export class AuthRepository implements IAuthRepository { return Result.err(e); } + const deviceId = await deviceIdentifier.getDeviceId(); const guestResult = await this.guestLogin(deviceId); if (Result.isOk(guestResult)) return Result.ok(undefined); diff --git a/src/data/repositories/interfaces/iauth_repository.ts b/src/data/repositories/interfaces/iauth_repository.ts index 3643a370..e89838b2 100644 --- a/src/data/repositories/interfaces/iauth_repository.ts +++ b/src/data/repositories/interfaces/iauth_repository.ts @@ -41,16 +41,9 @@ export interface IAuthRepository { /** 发送邮箱验证码。 */ sendCode(email: string): Promise>; - /** - * 退出登录:先调 API 注销服务端会话,无论成败都清本地登录态。 - */ + /** 退出真实用户登录并恢复游客态。 */ logout(): Promise>; - /** - * 退出真实用户登录并恢复游客态。 - */ - logoutToGuest(deviceId: string): Promise>; - /** 游客登录:使用 deviceId 换取 guest token。 */ guestLogin(deviceId: string): Promise>; diff --git a/src/stores/auth/auth-actors.ts b/src/stores/auth/auth-actors.ts index 5e351458..a4475769 100644 --- a/src/stores/auth/auth-actors.ts +++ b/src/stores/auth/auth-actors.ts @@ -96,8 +96,7 @@ export const oauthSignInActor = fromPromise(async ({ input } export const logoutActor = fromPromise(async () => { const authRepo = getAuthRepository(); - const deviceId = await deviceIdentifier.getDeviceId(); - const result = await authRepo.logoutToGuest(deviceId); + const result = await authRepo.logout(); if (Result.isErr(result)) throw result.error; return "guest" as LoginStatus; });