feat(auth): mark test accounts in login requests

This commit is contained in:
2026-06-26 14:05:53 +08:00
parent 8cb60ed7e3
commit abf6d5ae88
16 changed files with 78 additions and 36 deletions
+56 -34
View File
@@ -28,7 +28,7 @@ import {
SendCodeRequest,
} from "@/data/dto/auth";
import { User } from "@/data/dto/user";
import { Result, Logger } from "@/utils";
import { AppEnvUtil, Result, Logger } 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";
@@ -37,6 +37,7 @@ const log = new Logger("DataRepositoriesAuthRepository");
/** 硬编码平台名,对齐 Dart `PlatformUtil.platformName.toLowerCase()`Web 平台)。 */
const WEB_PLATFORM = "web";
const TEST_ACCOUNT_FLAG = "isTestAccount";
export class AuthRepository implements IAuthRepository {
constructor(
@@ -59,13 +60,15 @@ export class AuthRepository implements IAuthRepository {
}): Promise<Result<void>> {
return Result.wrap(async () => {
await this.api.register(
RegisterRequest.from({
username: input.username,
email: input.email,
password: input.password,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
RegisterRequest.from(
withTestAccountFlag({
username: input.username,
email: input.email,
password: input.password,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
),
);
});
}
@@ -79,13 +82,15 @@ export class AuthRepository implements IAuthRepository {
}): Promise<Result<LoginResponse>> {
return Result.wrap(async () => {
const response = await this.api.emailLogin(
LoginRequest.from({
email: input.email ?? "",
username: input.username ?? "",
password: input.password,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
LoginRequest.from(
withTestAccountFlag({
email: input.email ?? "",
username: input.username ?? "",
password: input.password,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
),
);
await this._saveLoginData(response);
return response;
@@ -131,7 +136,7 @@ export class AuthRepository implements IAuthRepository {
});
return Result.wrap(async () => {
const response = await this.api.guestLogin(
GuestLoginRequest.from({ deviceId }),
GuestLoginRequest.from(withTestAccountFlag({ deviceId })),
);
log.debug("[AuthRepository.guestLogin] API call SUCCESS (Zod parsed)", {
hasToken: !!response.token,
@@ -160,11 +165,13 @@ export class AuthRepository implements IAuthRepository {
}): Promise<Result<LoginResponse>> {
return this._socialLogin(() =>
this.api.googleLogin(
GoogleLoginRequest.from({
idToken: input.idToken,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
GoogleLoginRequest.from(
withTestAccountFlag({
idToken: input.idToken,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
),
),
);
}
@@ -176,11 +183,13 @@ export class AuthRepository implements IAuthRepository {
}): Promise<Result<LoginResponse>> {
return this._socialLogin(() =>
this.api.facebookLogin(
FacebookLoginRequest.from({
accessToken: input.accessToken,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
FacebookLoginRequest.from(
withTestAccountFlag({
accessToken: input.accessToken,
platform: WEB_PLATFORM,
guestId: input.guestId ?? "",
}),
),
),
);
}
@@ -192,10 +201,12 @@ export class AuthRepository implements IAuthRepository {
}): Promise<Result<LoginResponse>> {
return this._socialLogin(() =>
this.api.facebookIdLogin(
FbIdLoginRequest.from({
fbId: input.fbId,
avatarUrl: input.avatarUrl ?? "",
}),
FbIdLoginRequest.from(
withTestAccountFlag({
fbId: input.fbId,
avatarUrl: input.avatarUrl ?? "",
}),
),
),
);
}
@@ -208,10 +219,12 @@ export class AuthRepository implements IAuthRepository {
async appleLogin(identityToken: string): Promise<Result<LoginResponse>> {
return this._socialLogin(() =>
this.api.appleLogin(
AppleLoginRequest.from({
identityToken,
platform: WEB_PLATFORM,
}),
AppleLoginRequest.from(
withTestAccountFlag({
identityToken,
platform: WEB_PLATFORM,
}),
),
),
);
}
@@ -305,3 +318,12 @@ export const authRepository = new AuthRepository(
AuthStorage.getInstance(),
UserStorage.getInstance(),
);
function withTestAccountFlag<T extends Record<string, unknown>>(
payload: T,
): T & { isTestAccount: boolean } {
return {
...payload,
[TEST_ACCOUNT_FLAG]: !AppEnvUtil.isProduction(),
};
}