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 DEFAULT_LANG = 'en_US';
export const CURRENT_LANG_STORAGE_KEY = '_a_lang_'; export const CURRENT_LANG_STORAGE_KEY = '_a_lang_';
export const LANG_RESOURCE_STORAGE_KEY = '_a_lang_r_'; 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 LOGGED_TOKEN_STORAGE_KEY = '_a_ltk_';
export const REDIRECT_PATH_STORAGE_KEY = '_a_rp_'; export const REDIRECT_PATH_STORAGE_KEY = '_a_rp_';
export const CAPTCHA_CODE_STORAGE_KEY = '_a_captcha_'; export const CAPTCHA_CODE_STORAGE_KEY = '_a_captcha_';

View File

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

View File

@ -131,6 +131,7 @@ export const resendEmail = (params?: Type.ImgCodeReq) => {
* @returns {UserInfo} * @returns {UserInfo}
*/ */
export const getLoggedUserInfo = () => { 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'); 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 type { UserInfoRes } from '@/common/interface';
import Storage from '@/utils/storage'; import Storage from '@/utils/storage';
import { import { LOGGED_TOKEN_STORAGE_KEY } from '@/common/constants';
LOGGED_USER_STORAGE_KEY,
LOGGED_TOKEN_STORAGE_KEY,
} from '@/common/constants';
interface UserInfoStore { interface UserInfoStore {
user: UserInfoRes; user: UserInfoRes;
update: (params: UserInfoRes) => void; update: (params: UserInfoRes) => void;
clear: () => void; clear: (removeToken?: boolean) => void;
} }
const initUser: UserInfoRes = { const initUser: UserInfoRes = {
@ -39,14 +36,14 @@ const loggedUserInfo = create<UserInfoStore>((set) => ({
} }
set(() => { set(() => {
Storage.set(LOGGED_TOKEN_STORAGE_KEY, params.access_token); Storage.set(LOGGED_TOKEN_STORAGE_KEY, params.access_token);
Storage.set(LOGGED_USER_STORAGE_KEY, params);
return { user: params }; return { user: params };
}); });
}, },
clear: () => clear: (removeToken = true) =>
set(() => { set(() => {
Storage.remove(LOGGED_TOKEN_STORAGE_KEY); if (removeToken) {
Storage.remove(LOGGED_USER_STORAGE_KEY); Storage.remove(LOGGED_TOKEN_STORAGE_KEY);
}
return { user: initUser }; return { user: initUser };
}), }),
})); }));

View File

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