Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

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

haitaoo8d4bbfc90b15
diff --git a/ui/src/common/constants.ts b/ui/src/common/constants.ts
index 320ae302..7a540ac9 100644
--- a/ui/src/common/constants.ts
+++ b/ui/src/common/constants.ts
@@ -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_';
diff --git a/ui/src/pages/Users/Settings/Profile/index.tsx b/ui/src/pages/Users/Settings/Profile/index.tsx
index 6fb3e02f..8728ba01 100644
--- a/ui/src/pages/Users/Settings/Profile/index.tsx
+++ b/ui/src/pages/Users/Settings/Profile/index.tsx
@@ -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;
diff --git a/ui/src/services/common.ts b/ui/src/services/common.ts
index a7b41552..5701adfa 100644
--- a/ui/src/services/common.ts
+++ b/ui/src/services/common.ts
@@ -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');
};
diff --git a/ui/src/stores/loggedUserInfo.ts b/ui/src/stores/loggedUserInfo.ts
index a4ed9841..2f7a35f4 100644
--- a/ui/src/stores/loggedUserInfo.ts
+++ b/ui/src/stores/loggedUserInfo.ts
@@ -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 };
}),
}));
diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.ts
index c27e4410..899fe5f4 100644
--- a/ui/src/utils/guard.ts
+++ b/ui/src/utils/guard.ts
@@ -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) {