feat(auth): persist login provider for restored sessions
This commit is contained in:
@@ -10,6 +10,8 @@ import {
|
||||
GuestLoginResponse,
|
||||
LoginRequest,
|
||||
LoginResponse,
|
||||
LoginStatus,
|
||||
type LoginStatus as LoginStatusT,
|
||||
RefreshTokenRequest,
|
||||
RefreshTokenResponse,
|
||||
RegisterRequest,
|
||||
@@ -79,7 +81,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
}),
|
||||
),
|
||||
);
|
||||
await this._saveLoginData(response);
|
||||
await this._saveLoginData(response, LoginStatus.Email);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
@@ -131,6 +133,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
userLastMessageAt: response.user?.lastMessageAt,
|
||||
});
|
||||
await this.storage.setGuestToken(response.token);
|
||||
await this.storage.setLoginProvider(LoginStatus.Guest);
|
||||
await this.storage.setDeviceId(deviceId);
|
||||
if (response.userId) {
|
||||
await this.userStorage.setUserId(response.userId);
|
||||
@@ -150,7 +153,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
idToken: string;
|
||||
guestId?: string;
|
||||
}): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(() =>
|
||||
return this._socialLogin(LoginStatus.Google, () =>
|
||||
this.api.googleLogin(
|
||||
GoogleLoginRequest.from(
|
||||
withTestAccountFlag({
|
||||
@@ -168,7 +171,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
accessToken: string;
|
||||
guestId?: string;
|
||||
}): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(() =>
|
||||
return this._socialLogin(LoginStatus.Facebook, () =>
|
||||
this.api.facebookLogin(
|
||||
FacebookLoginRequest.from(
|
||||
withTestAccountFlag({
|
||||
@@ -186,7 +189,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
fbId: string;
|
||||
avatarUrl?: string;
|
||||
}): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(() =>
|
||||
return this._socialLogin(LoginStatus.Facebook, () =>
|
||||
this.api.facebookIdLogin(
|
||||
FbIdLoginRequest.from(
|
||||
withTestAccountFlag({
|
||||
@@ -204,7 +207,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
* 尚未实现该端点,故仓库暂不暴露。等 `AuthApi` 补齐后再加。
|
||||
*/
|
||||
async appleLogin(identityToken: string): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(() =>
|
||||
return this._socialLogin(LoginStatus.Apple, () =>
|
||||
this.api.appleLogin(
|
||||
AppleLoginRequest.from(
|
||||
withTestAccountFlag({
|
||||
@@ -263,11 +266,12 @@ export class AuthRepository implements IAuthRepository {
|
||||
* `_saveLoginData` 内部全部 best-effort,存储写失败不抛错。
|
||||
*/
|
||||
private async _socialLogin(
|
||||
provider: LoginStatusT,
|
||||
call: () => Promise<LoginResponse>,
|
||||
): Promise<Result<LoginResponse>> {
|
||||
return Result.wrap(async () => {
|
||||
const response = await call();
|
||||
await this._saveLoginData(response);
|
||||
await this._saveLoginData(response, provider);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
@@ -277,11 +281,18 @@ export class AuthRepository implements IAuthRepository {
|
||||
* 全部 best-effort,错误用 `Logger.warn` 记录但不让登录「失败」——
|
||||
* 调用方已经从 API 拿到了 LoginResponse,缓存只是离线加速。
|
||||
*/
|
||||
private async _saveLoginData(data: LoginResponse): Promise<void> {
|
||||
private async _saveLoginData(
|
||||
data: LoginResponse,
|
||||
provider: LoginStatusT,
|
||||
): Promise<void> {
|
||||
const r1 = await this.storage.setLoginToken(data.token);
|
||||
if (!r1.success) {
|
||||
log.warn("[AuthRepository] setLoginToken failed", r1.error);
|
||||
}
|
||||
const providerResult = await this.storage.setLoginProvider(provider);
|
||||
if (!providerResult.success) {
|
||||
log.warn("[AuthRepository] setLoginProvider failed", providerResult.error);
|
||||
}
|
||||
if (data.refreshToken) {
|
||||
const r2 = await this.storage.setRefreshToken(data.refreshToken);
|
||||
if (!r2.success) {
|
||||
|
||||
Reference in New Issue
Block a user