Skip to content

Commit

Permalink
fix(projects): fix get user info when page reload
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 7, 2024
1 parent fe06b8c commit ff51b72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/router/guard/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,6 +161,8 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
return location;
}

await authStore.initUserInfo();

// initialize the auth route
await routeStore.initAuthRoute();

Expand Down
20 changes: 16 additions & 4 deletions src/store/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -125,6 +137,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
loginLoading,
resetStore,
login,
updateUserInfo
initUserInfo
};
});

0 comments on commit ff51b72

Please sign in to comment.