From 5531a68641fe510448b5aece4e615b4791b1fca2 Mon Sep 17 00:00:00 2001 From: Soybean Date: Thu, 6 Jun 2024 20:22:28 +0800 Subject: [PATCH] feat(projects): get user info in router guard and remove in localStorage. close #459 --- src/router/guard/route.ts | 4 ++++ src/store/modules/auth/index.ts | 31 +++++++++++++++++++++++-------- src/store/modules/auth/shared.ts | 19 ------------------- src/typings/storage.d.ts | 2 -- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/router/guard/route.ts b/src/router/guard/route.ts index 03a5121d4..0a00ac71f 100644 --- a/src/router/guard/route.ts +++ b/src/router/guard/route.ts @@ -92,6 +92,7 @@ export function createRouteGuard(router: Router) { * @param to to route */ async function initRoute(to: RouteLocationNormalized): Promise { + const authStore = useAuthStore(); const routeStore = useRouteStore(); const notFoundRoute: RouteKey = 'not-found'; @@ -125,6 +126,9 @@ async function initRoute(to: RouteLocationNormalized): Promise { const route = useRoute(); @@ -18,7 +18,12 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => { const token = ref(getToken()); - const userInfo: Api.Auth.UserInfo = reactive(getUserInfo()); + const userInfo: Api.Auth.UserInfo = reactive({ + userId: '', + userName: '', + roles: [], + buttons: [] + }); /** is super role in static route */ const isStaticSuper = computed(() => { @@ -87,14 +92,23 @@ 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(); + + if (pass) { + token.value = loginToken.token; + + return true; + } + + return false; + } + + async function updateUserInfo() { const { data: info, error } = await fetchGetUserInfo(); if (!error) { - // 2. store user info - localStg.set('userInfo', info); - - // 3. update store - token.value = loginToken.token; + // update store Object.assign(userInfo, info); return true; @@ -110,6 +124,7 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => { isLogin, loginLoading, resetStore, - login + login, + updateUserInfo }; }); diff --git a/src/store/modules/auth/shared.ts b/src/store/modules/auth/shared.ts index 4681c9222..5cf88522f 100644 --- a/src/store/modules/auth/shared.ts +++ b/src/store/modules/auth/shared.ts @@ -5,27 +5,8 @@ export function getToken() { return localStg.get('token') || ''; } -/** Get user info */ -export function getUserInfo() { - const emptyInfo: Api.Auth.UserInfo = { - userId: '', - userName: '', - roles: [], - buttons: [] - }; - const userInfo = localStg.get('userInfo') || emptyInfo; - - // fix new property: buttons, this will be removed in the next version `1.1.0` - if (!userInfo.buttons) { - userInfo.buttons = []; - } - - return userInfo; -} - /** Clear auth storage */ export function clearAuthStorage() { localStg.remove('token'); localStg.remove('refreshToken'); - localStg.remove('userInfo'); } diff --git a/src/typings/storage.d.ts b/src/typings/storage.d.ts index 1eb0bf26e..0d52f33c5 100644 --- a/src/typings/storage.d.ts +++ b/src/typings/storage.d.ts @@ -18,8 +18,6 @@ declare namespace StorageType { mixSiderFixed: CommonType.YesOrNo; /** The refresh token */ refreshToken: string; - /** The user info */ - userInfo: Api.Auth.UserInfo; /** The theme color */ themeColor: string; /** The theme settings */