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 // the auth route is initialized
// it is not the "not-found" route, then it is allowed to access // it is not the "not-found" route, then it is allowed to access
if (routeStore.isInitAuthRoute && !isNotFoundRoute) { if (routeStore.isInitAuthRoute && !isNotFoundRoute) {
// update user info
await authStore.updateUserInfo();
return null; return null;
} }
// it is captured by the "not-found" route, then check whether the route exists // 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; return location;
} }
await authStore.initUserInfo();
// initialize the auth route // initialize the auth route
await routeStore.initAuthRoute(); await routeStore.initAuthRoute();

View File

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