refactor(auth): fold guest restore into logout
This commit is contained in:
@@ -19,7 +19,13 @@ import {
|
|||||||
SendCodeRequest,
|
SendCodeRequest,
|
||||||
} from "@/data/dto/auth";
|
} from "@/data/dto/auth";
|
||||||
import { User } from "@/data/dto/user";
|
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 type { IAuthRepository } from "@/data/repositories/interfaces";
|
||||||
import { AuthStorage, type IAuthStorage } from "@/data/storage/auth";
|
import { AuthStorage, type IAuthStorage } from "@/data/storage/auth";
|
||||||
import { UserStorage, type IUserStorage } from "@/data/storage/user";
|
import { UserStorage, type IUserStorage } from "@/data/storage/user";
|
||||||
@@ -95,35 +101,15 @@ export class AuthRepository implements IAuthRepository {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 退出登录:先调 API 注销服务端会话,无论成败都清本地登录态。
|
|
||||||
* 强制清本地的逻辑:用户即使在 API 失败时也想从设备上登出。
|
|
||||||
*/
|
|
||||||
async logout(): Promise<Result<void>> {
|
|
||||||
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 / deviceId。退出后会尝试重新跑游客登录,以便恢复游客 userId;
|
||||||
* guestToken,则降级回本地游客态,避免用户从聊天页被打回 splash。
|
* 如果游客登录接口失败但本地仍有 guestToken,则降级回本地游客态,避免
|
||||||
|
* 用户从聊天页被打回 splash。
|
||||||
*/
|
*/
|
||||||
async logoutToGuest(deviceId: string): Promise<Result<void>> {
|
async logout(): Promise<Result<void>> {
|
||||||
try {
|
try {
|
||||||
await this.api.logout();
|
await this.api.logout();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -137,6 +123,7 @@ export class AuthRepository implements IAuthRepository {
|
|||||||
return Result.err(e);
|
return Result.err(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deviceId = await deviceIdentifier.getDeviceId();
|
||||||
const guestResult = await this.guestLogin(deviceId);
|
const guestResult = await this.guestLogin(deviceId);
|
||||||
if (Result.isOk(guestResult)) return Result.ok(undefined);
|
if (Result.isOk(guestResult)) return Result.ok(undefined);
|
||||||
|
|
||||||
|
|||||||
@@ -41,16 +41,9 @@ export interface IAuthRepository {
|
|||||||
/** 发送邮箱验证码。 */
|
/** 发送邮箱验证码。 */
|
||||||
sendCode(email: string): Promise<Result<void>>;
|
sendCode(email: string): Promise<Result<void>>;
|
||||||
|
|
||||||
/**
|
/** 退出真实用户登录并恢复游客态。 */
|
||||||
* 退出登录:先调 API 注销服务端会话,无论成败都清本地登录态。
|
|
||||||
*/
|
|
||||||
logout(): Promise<Result<void>>;
|
logout(): Promise<Result<void>>;
|
||||||
|
|
||||||
/**
|
|
||||||
* 退出真实用户登录并恢复游客态。
|
|
||||||
*/
|
|
||||||
logoutToGuest(deviceId: string): Promise<Result<void>>;
|
|
||||||
|
|
||||||
/** 游客登录:使用 deviceId 换取 guest token。 */
|
/** 游客登录:使用 deviceId 换取 guest token。 */
|
||||||
guestLogin(deviceId: string): Promise<Result<GuestLoginResponse>>;
|
guestLogin(deviceId: string): Promise<Result<GuestLoginResponse>>;
|
||||||
|
|
||||||
|
|||||||
@@ -96,8 +96,7 @@ export const oauthSignInActor = fromPromise<void, AuthProvider>(async ({ input }
|
|||||||
|
|
||||||
export const logoutActor = fromPromise<LoginStatus>(async () => {
|
export const logoutActor = fromPromise<LoginStatus>(async () => {
|
||||||
const authRepo = getAuthRepository();
|
const authRepo = getAuthRepository();
|
||||||
const deviceId = await deviceIdentifier.getDeviceId();
|
const result = await authRepo.logout();
|
||||||
const result = await authRepo.logoutToGuest(deviceId);
|
|
||||||
if (Result.isErr(result)) throw result.error;
|
if (Result.isErr(result)) throw result.error;
|
||||||
return "guest" as LoginStatus;
|
return "guest" as LoginStatus;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user