mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
fix: stay loading when refresh token fail (#1194)
This commit is contained in:
@@ -13,6 +13,7 @@ export enum MessageCode {
|
||||
getBlobFailed,
|
||||
leaveWorkspaceFailed,
|
||||
downloadWorkspaceFailed,
|
||||
refreshTokenError,
|
||||
}
|
||||
|
||||
export const messages = {
|
||||
@@ -58,4 +59,7 @@ export const messages = {
|
||||
[MessageCode.downloadWorkspaceFailed]: {
|
||||
message: 'Download workspace failed',
|
||||
},
|
||||
[MessageCode.refreshTokenError]: {
|
||||
message: 'Refresh token failed',
|
||||
},
|
||||
};
|
||||
|
||||
@@ -11,7 +11,9 @@ import {
|
||||
import { decode } from 'js-base64';
|
||||
import { KyInstance } from 'ky/distribution/types/ky';
|
||||
|
||||
import { MessageCenter } from '../../../message';
|
||||
import { storage } from '../storage';
|
||||
import { RequestError } from './request-error';
|
||||
|
||||
export interface AccessTokenMessage {
|
||||
created_at: number;
|
||||
@@ -38,6 +40,9 @@ type LoginResponse = {
|
||||
|
||||
// TODO: organize storage keys in a better way
|
||||
const AFFINE_LOGIN_STORAGE_KEY = 'affine:login';
|
||||
const messageCenter = MessageCenter.getInstance();
|
||||
const sendMessage = messageCenter.getMessageSender('affine');
|
||||
const { messageCode } = MessageCenter;
|
||||
|
||||
/**
|
||||
* Use refresh token to get a new access token (JWT)
|
||||
@@ -98,9 +103,17 @@ export class GoogleAuth {
|
||||
}
|
||||
|
||||
async initToken(token: string) {
|
||||
const res = await this._doLogin({ token, type: 'Google' });
|
||||
this.setLogin(res);
|
||||
return this._user;
|
||||
try {
|
||||
const res = await this._doLogin({
|
||||
token,
|
||||
type: 'Google',
|
||||
});
|
||||
this.setLogin(res);
|
||||
return this._user;
|
||||
} catch (error) {
|
||||
sendMessage(messageCode.loginError);
|
||||
throw new RequestError('Login failed', error);
|
||||
}
|
||||
}
|
||||
|
||||
async refreshToken(refreshToken?: string) {
|
||||
@@ -117,8 +130,9 @@ export class GoogleAuth {
|
||||
this.setLogin(res);
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
this._logger.warn('Failed to refresh token');
|
||||
} catch (error) {
|
||||
sendMessage(messageCode.refreshTokenError);
|
||||
throw new RequestError('Refresh token failed', error);
|
||||
} finally {
|
||||
// clear on settled
|
||||
this._padding = undefined;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
export class RequestError extends Error {
|
||||
constructor(message: string, cause: unknown | null = null) {
|
||||
super(message);
|
||||
this.name = 'RequestError';
|
||||
this.cause = cause;
|
||||
}
|
||||
}
|
||||
|
||||
export default RequestError;
|
||||
@@ -3,22 +3,14 @@ import { KyInstance } from 'ky/distribution/types/ky';
|
||||
import { MessageCenter } from '../../../message';
|
||||
import { createBlocksuiteWorkspace as _createBlocksuiteWorkspace } from '../../../utils';
|
||||
import { GoogleAuth } from './google';
|
||||
import RequestError from './request-error';
|
||||
import type { User } from './user';
|
||||
|
||||
const messageCenter = MessageCenter.getInstance();
|
||||
|
||||
const sendMessage = messageCenter.getMessageSender('affine');
|
||||
|
||||
const { messageCode } = MessageCenter;
|
||||
|
||||
class RequestError extends Error {
|
||||
constructor(message: string, cause: unknown | null = null) {
|
||||
super(message);
|
||||
this.name = 'RequestError';
|
||||
this.cause = cause;
|
||||
}
|
||||
}
|
||||
|
||||
export interface GetWorkspaceDetailParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user