fix(auth): return to guest session on logout

This commit is contained in:
2026-07-03 16:14:04 +08:00
parent b22f23bcc4
commit 8f17ea9c9f
11 changed files with 97 additions and 26 deletions
+40
View File
@@ -116,6 +116,46 @@ export class AuthRepository implements IAuthRepository {
}
}
/**
* 退出真实用户登录并恢复游客态。
*
* 与 `logout()` 不同,这里不会删除 guestToken / deviceId。退出后会尝试
* 重新跑游客登录,以便恢复游客 userId;如果游客登录接口失败但本地仍有
* guestToken,则降级回本地游客态,避免用户从聊天页被打回 splash。
*/
async logoutToGuest(deviceId: string): Promise<Result<void>> {
try {
await this.api.logout();
} catch (e) {
log.warn("[AuthRepository] logout API failed, restoring guest anyway", e);
}
try {
await this.storage.clearBusinessAuthData();
await this.userStorage.clearUserData();
} catch (e) {
return Result.err(e);
}
const guestResult = await this.guestLogin(deviceId);
if (Result.isOk(guestResult)) return Result.ok(undefined);
const hasGuestToken = await this.storage.hasGuestToken();
if (hasGuestToken.success && hasGuestToken.data) {
const providerResult = await this.storage.setLoginProvider(
LoginStatus.Guest,
);
if (Result.isErr(providerResult)) return providerResult;
log.warn(
"[AuthRepository] guest login failed after logout, falling back to local guest token",
guestResult.error,
);
return Result.ok(undefined);
}
return Result.err(guestResult.error);
}
/**
* 游客登录:使用 deviceId 换取 guest token。
* 成功后写 guest token + deviceId + userId。
@@ -46,6 +46,11 @@ export interface IAuthRepository {
*/
logout(): Promise<Result<void>>;
/**
* 退出真实用户登录并恢复游客态。
*/
logoutToGuest(deviceId: string): Promise<Result<void>>;
/** 游客登录:使用 deviceId 换取 guest token。 */
guestLogin(deviceId: string): Promise<Result<GuestLoginResponse>>;