fix(LoggedUserInfo): Make the logic for getting information about the currently logged in user more robust

This commit is contained in:
haitaoo 2023-03-30 10:53:33 +08:00
parent be22b49139
commit 8d4bbfc90b
5 changed files with 8 additions and 20 deletions

View File

@ -2,7 +2,6 @@ export const DEFAULT_SITE_NAME = 'Answer';
export const DEFAULT_LANG = 'en_US';
export const CURRENT_LANG_STORAGE_KEY = '_a_lang_';
export const LANG_RESOURCE_STORAGE_KEY = '_a_lang_r_';
export const LOGGED_USER_STORAGE_KEY = '_a_lui_';
export const LOGGED_TOKEN_STORAGE_KEY = '_a_ltk_';
export const REDIRECT_PATH_STORAGE_KEY = '_a_rp_';
export const CAPTCHA_CODE_STORAGE_KEY = '_a_captcha_';

View File

@ -228,7 +228,6 @@ const Index: React.FC = () => {
};
const getProfile = () => {
console.log('getProfile@Profile');
getLoggedUserInfo().then((res) => {
formData.display_name.value = res.display_name;
formData.username.value = res.username;

View File

@ -131,6 +131,7 @@ export const resendEmail = (params?: Type.ImgCodeReq) => {
* @returns {UserInfo}
*/
export const getLoggedUserInfo = () => {
// TODO: After merging `1.0.8` versions, set the `passingError` parameter for this request
return request.get<Type.UserInfoRes>('/answer/api/v1/user/info');
};

View File

@ -2,15 +2,12 @@ import create from 'zustand';
import type { UserInfoRes } from '@/common/interface';
import Storage from '@/utils/storage';
import {
LOGGED_USER_STORAGE_KEY,
LOGGED_TOKEN_STORAGE_KEY,
} from '@/common/constants';
import { LOGGED_TOKEN_STORAGE_KEY } from '@/common/constants';
interface UserInfoStore {
user: UserInfoRes;
update: (params: UserInfoRes) => void;
clear: () => void;
clear: (removeToken?: boolean) => void;
}
const initUser: UserInfoRes = {
@ -39,14 +36,14 @@ const loggedUserInfo = create<UserInfoStore>((set) => ({
}
set(() => {
Storage.set(LOGGED_TOKEN_STORAGE_KEY, params.access_token);
Storage.set(LOGGED_USER_STORAGE_KEY, params);
return { user: params };
});
},
clear: () =>
clear: (removeToken = true) =>
set(() => {
Storage.remove(LOGGED_TOKEN_STORAGE_KEY);
Storage.remove(LOGGED_USER_STORAGE_KEY);
if (removeToken) {
Storage.remove(LOGGED_TOKEN_STORAGE_KEY);
}
return { user: initUser };
}),
}));

View File

@ -11,9 +11,7 @@ import {
loginToContinueStore,
} from '@/stores';
import { RouteAlias } from '@/router/alias';
import { LOGGED_USER_STORAGE_KEY } from '@/common/constants';
import Storage from './storage';
import { setupAppLanguage, setupAppTimeZone } from './localize';
import { floppyNavigation } from './floppyNavigation';
import { pullUcAgent, getLoginUrl, getSignUpUrl } from './userCenter';
@ -94,13 +92,7 @@ export const pullLoggedUser = async (forceRePull = false) => {
pluTimestamp = Date.now();
const loggedUserInfo = await getLoggedUserInfo().catch((ex) => {
pluTimestamp = 0;
if (!deriveLoginState().isLogged) {
// load fallback userInfo from local storage
const storageLoggedUserInfo = Storage.get(LOGGED_USER_STORAGE_KEY);
if (storageLoggedUserInfo) {
loggedUserInfoStore.getState().update(storageLoggedUserInfo);
}
}
loggedUserInfoStore.getState().clear(false);
console.error(ex);
});
if (loggedUserInfo) {