fix(projects): fix get user info when page reload

This commit is contained in:
Soybean 2024-06-07 11:28:49 +08:00
parent fe06b8c499
commit ff51b72dac
2 changed files with 18 additions and 7 deletions

View File

@ -126,9 +126,6 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
// the auth route is initialized
// it is not the "not-found" route, then it is allowed to access
if (routeStore.isInitAuthRoute && !isNotFoundRoute) {
// update user info
await authStore.updateUserInfo();
return null;
}
// it is captured by the "not-found" route, then check whether the route exists
@ -164,6 +161,8 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
return location;
}
await authStore.initUserInfo();
// initialize the auth route
await routeStore.initAuthRoute();

View File

@ -92,8 +92,8 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
localStg.set('token', loginToken.token);
localStg.set('refreshToken', loginToken.refreshToken);
// 2. get user info and update store
const pass = await updateUserInfo();
// 2. get user info
const pass = await getUserInfo();
if (pass) {
token.value = loginToken.token;
@ -104,7 +104,7 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
return false;
}
async function updateUserInfo() {
async function getUserInfo() {
const { data: info, error } = await fetchGetUserInfo();
if (!error) {
@ -117,6 +117,18 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
return false;
}
async function initUserInfo() {
const hasToken = getToken();
if (hasToken) {
const pass = await getUserInfo();
if (!pass) {
resetStore();
}
}
}
return {
token,
userInfo,
@ -125,6 +137,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
loginLoading,
resetStore,
login,
updateUserInfo
initUserInfo
};
});