Skip to content

Commit

Permalink
feat(projects): get user info in router guard and remove in localStor…
Browse files Browse the repository at this point in the history
…age. close #459
  • Loading branch information
honghuangdc committed Jun 6, 2024
1 parent 584cd54 commit 5531a68
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/router/guard/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function createRouteGuard(router: Router) {
* @param to to route
*/
async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw | null> {
const authStore = useAuthStore();
const routeStore = useRouteStore();

const notFoundRoute: RouteKey = 'not-found';
Expand Down Expand Up @@ -125,6 +126,9 @@ 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
31 changes: 23 additions & 8 deletions src/store/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fetchGetUserInfo, fetchLogin } from '@/service/api';
import { localStg } from '@/utils/storage';
import { $t } from '@/locales';
import { useRouteStore } from '../route';
import { clearAuthStorage, getToken, getUserInfo } from './shared';
import { clearAuthStorage, getToken } from './shared';

export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
const route = useRoute();
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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;
Expand All @@ -110,6 +124,7 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
isLogin,
loginLoading,
resetStore,
login
login,
updateUserInfo
};
});
19 changes: 0 additions & 19 deletions src/store/modules/auth/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
2 changes: 0 additions & 2 deletions src/typings/storage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 5531a68

Please sign in to comment.