feat(auth): add facebook identity psid login
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
import { ExceptionHandler } from "@/core/errors";
|
||||
import { ApiError, AuthApi, authApi, ErrorCode } from "@/data/services/api";
|
||||
import {
|
||||
FacebookIdentityRequest,
|
||||
FacebookPsidLoginRequest,
|
||||
FacebookLoginRequest,
|
||||
FbIdLoginRequest,
|
||||
GoogleLoginRequest,
|
||||
@@ -230,6 +232,73 @@ export class AuthRepository implements IAuthRepository {
|
||||
);
|
||||
}
|
||||
|
||||
/** 绑定 Facebook ASID / PSID 到当前登录用户。 */
|
||||
async bindFacebookIdentity(input: {
|
||||
asid?: string;
|
||||
psid?: string;
|
||||
}): Promise<Result<void>> {
|
||||
if (!input.asid && !input.psid) return Result.ok(undefined);
|
||||
|
||||
return Result.wrap(async () => {
|
||||
const response = await this.api.bindFacebookIdentity(
|
||||
FacebookIdentityRequest.from({
|
||||
asid: input.asid ?? "",
|
||||
psid: input.psid ?? "",
|
||||
}),
|
||||
);
|
||||
const data = response.toJson();
|
||||
await this._saveFacebookIdentity({
|
||||
asid: data.fbAsid,
|
||||
psid: data.fbPsid,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/** 通过 Facebook PSID 直接登录或返回游客态。 */
|
||||
async facebookPsidLogin(input: {
|
||||
psid: string;
|
||||
deviceId?: string;
|
||||
bindToGuest?: boolean;
|
||||
}): Promise<Result<LoginStatusT>> {
|
||||
return Result.wrap(async () => {
|
||||
const response = await this.api.facebookPsidLogin(
|
||||
FacebookPsidLoginRequest.from({
|
||||
psid: input.psid,
|
||||
deviceId: input.deviceId ?? "",
|
||||
bindToGuest: input.bindToGuest ?? true,
|
||||
}),
|
||||
);
|
||||
const data = response.toJson();
|
||||
await this._saveFacebookIdentity({
|
||||
asid: data.fbAsid,
|
||||
psid: data.fbPsid,
|
||||
});
|
||||
|
||||
if (data.isGuest || !data.hasCompleteFacebookIdentity) {
|
||||
await this.storage.setGuestToken(data.token);
|
||||
await this.storage.setLoginProvider(LoginStatus.Guest);
|
||||
if (input.deviceId) {
|
||||
await this.storage.setDeviceId(input.deviceId);
|
||||
}
|
||||
const userId = data.userId || data.user?.id || "";
|
||||
if (userId) {
|
||||
await this.userStorage.setUserId(userId);
|
||||
}
|
||||
return LoginStatus.Guest;
|
||||
}
|
||||
|
||||
await this._saveLoginData(
|
||||
LoginResponse.from({
|
||||
token: data.token,
|
||||
refreshToken: data.refreshToken,
|
||||
user: data.user ?? { id: data.userId },
|
||||
}),
|
||||
LoginStatus.Facebook,
|
||||
);
|
||||
return LoginStatus.Facebook;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新 token:先读本地的 refresh token,空则直接返回错误;
|
||||
* 调用 API 成功后写回新的 login token + refresh token。
|
||||
@@ -325,6 +394,24 @@ export class AuthRepository implements IAuthRepository {
|
||||
log.warn("[AuthRepository] setUserId failed", r4.error);
|
||||
}
|
||||
}
|
||||
|
||||
private async _saveFacebookIdentity(input: {
|
||||
asid?: string;
|
||||
psid?: string;
|
||||
}): Promise<void> {
|
||||
if (input.asid) {
|
||||
const r = await this.storage.setAsid(input.asid);
|
||||
if (!r.success) {
|
||||
log.warn("[AuthRepository] setAsid failed", r.error);
|
||||
}
|
||||
}
|
||||
if (input.psid) {
|
||||
const r = await this.storage.setPsid(input.psid);
|
||||
if (!r.success) {
|
||||
log.warn("[AuthRepository] setPsid failed", r.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 全局懒单例。 */
|
||||
|
||||
Reference in New Issue
Block a user