Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: where pages could not be refreshed when login expired #4204

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/backend-mock/utils/jwt-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface UserPayload extends UserInfo {
}

export function generateAccessToken(user: UserInfo) {
return jwt.sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '2h' });
return jwt.sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '1d' });
}

export function generateRefreshToken(user: UserInfo) {
Expand Down
5 changes: 4 additions & 1 deletion apps/web-antd/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function createRequestClient(baseURL: string) {
const accessStore = useAccessStore();
const authStore = useAuthStore();
accessStore.setAccessToken(null);
if (preferences.app.loginExpiredMode === 'modal') {
if (
preferences.app.loginExpiredMode === 'modal' &&
accessStore.isAccessChecked
) {
accessStore.setLoginExpired(true);
} else {
await authStore.logout();
Expand Down
5 changes: 2 additions & 3 deletions apps/web-antd/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ function setupAccessGuard(router: Router) {
return to;
}

const accessRoutes = accessStore.accessRoutes;

// 是否已经生成过动态路由
if (accessRoutes && accessRoutes.length > 0) {
if (accessStore.isAccessChecked) {
return true;
}

Expand All @@ -115,6 +113,7 @@ function setupAccessGuard(router: Router) {
// 保存菜单信息和路由信息
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
const redirectPath = (from.query.redirect ?? to.fullPath) as string;

return {
Expand Down
5 changes: 4 additions & 1 deletion apps/web-ele/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function createRequestClient(baseURL: string) {
const accessStore = useAccessStore();
const authStore = useAuthStore();
accessStore.setAccessToken(null);
if (preferences.app.loginExpiredMode === 'modal') {
if (
preferences.app.loginExpiredMode === 'modal' &&
accessStore.isAccessChecked
) {
accessStore.setLoginExpired(true);
} else {
await authStore.logout();
Expand Down
5 changes: 2 additions & 3 deletions apps/web-ele/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ function setupAccessGuard(router: Router) {
return to;
}

const accessRoutes = accessStore.accessRoutes;

// 是否已经生成过动态路由
if (accessRoutes && accessRoutes.length > 0) {
if (accessStore.isAccessChecked) {
return true;
}

Expand All @@ -115,6 +113,7 @@ function setupAccessGuard(router: Router) {
// 保存菜单信息和路由信息
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
const redirectPath = (from.query.redirect ?? to.fullPath) as string;

return {
Expand Down
5 changes: 4 additions & 1 deletion apps/web-naive/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ function createRequestClient(baseURL: string) {
const accessStore = useAccessStore();
const authStore = useAuthStore();
accessStore.setAccessToken(null);
if (preferences.app.loginExpiredMode === 'modal') {
if (
preferences.app.loginExpiredMode === 'modal' &&
accessStore.isAccessChecked
) {
accessStore.setLoginExpired(true);
} else {
await authStore.logout();
Expand Down
6 changes: 2 additions & 4 deletions apps/web-naive/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,10 @@ function setupAccessGuard(router: Router) {
return to;
}

const accessRoutes = accessStore.accessRoutes;

// 是否已经生成过动态路由
if (accessRoutes && accessRoutes.length > 0) {
if (accessStore.isAccessChecked) {
return true;
}

// 生成路由表
// 当前登录用户拥有的角色标识列表
const userInfo = userStore.userInfo || (await authStore.fetchUserInfo());
Expand All @@ -115,6 +112,7 @@ function setupAccessGuard(router: Router) {
// 保存菜单信息和路由信息
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
const redirectPath = (from.query.redirect ?? to.fullPath) as string;

return {
Expand Down
5 changes: 4 additions & 1 deletion docs/src/guide/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ function createRequestClient(baseURL: string) {
const accessStore = useAccessStore();
const authStore = useAuthStore();
accessStore.setAccessToken(null);
if (preferences.app.loginExpiredMode === 'modal') {
if (
preferences.app.loginExpiredMode === 'modal' &&
accessStore.isAccessChecked
) {
accessStore.setLoginExpired(true);
} else {
await authStore.logout();
Expand Down
2 changes: 1 addition & 1 deletion packages/@core/preferences/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const defaultPreferences: Preferences = {
isMobile: false,
layout: 'sidebar-nav',
locale: 'zh-CN',
loginExpiredMode: 'modal',
loginExpiredMode: 'page',
name: 'Vben Admin',
preferencesButtonPosition: 'fixed',
watermark: false,
Expand Down
8 changes: 8 additions & 0 deletions packages/stores/src/modules/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface AccessState {
* 登录 accessToken
*/
accessToken: AccessToken;
/**
* 是否已经检查过权限
*/
isAccessChecked: boolean;
/**
* 登录是否过期
*/
Expand Down Expand Up @@ -49,6 +53,9 @@ export const useAccessStore = defineStore('core-access', {
setAccessToken(token: AccessToken) {
this.accessToken = token;
},
setIsAccessChecked(isAccessChecked: boolean) {
this.isAccessChecked = isAccessChecked;
},
setLoginExpired(loginExpired: boolean) {
this.loginExpired = loginExpired;
},
Expand All @@ -65,6 +72,7 @@ export const useAccessStore = defineStore('core-access', {
accessMenus: [],
accessRoutes: [],
accessToken: null,
isAccessChecked: false,
loginExpired: false,
refreshToken: null,
}),
Expand Down
5 changes: 4 additions & 1 deletion playground/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function createRequestClient(baseURL: string) {
const accessStore = useAccessStore();
const authStore = useAuthStore();
accessStore.setAccessToken(null);
if (preferences.app.loginExpiredMode === 'modal') {
if (
preferences.app.loginExpiredMode === 'modal' &&
accessStore.isAccessChecked
) {
accessStore.setLoginExpired(true);
} else {
await authStore.logout();
Expand Down
6 changes: 2 additions & 4 deletions playground/src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ function setupAccessGuard(router: Router) {
const accessStore = useAccessStore();
const userStore = useUserStore();
const authStore = useAuthStore();

// 基本路由,这些路由不需要进入权限拦截
if (coreRouteNames.includes(to.name as string)) {
if (to.path === LOGIN_PATH && accessStore.accessToken) {
Expand Down Expand Up @@ -92,10 +91,8 @@ function setupAccessGuard(router: Router) {
return to;
}

const accessRoutes = accessStore.accessRoutes;

// 是否已经生成过动态路由
if (accessRoutes && accessRoutes.length > 0) {
if (accessStore.isAccessChecked) {
return true;
}

Expand All @@ -115,6 +112,7 @@ function setupAccessGuard(router: Router) {
// 保存菜单信息和路由信息
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
const redirectPath = (from.query.redirect ?? to.fullPath) as string;

return {
Expand Down
Loading