Skip to content

Commit a1490c4

Browse files
committed
feat: 登录未过期跳转到 home
1 parent 452f1af commit a1490c4

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/app.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import defaultSettings from '../config/defaultSettings';
1010
import { errorConfig } from './requestErrorConfig';
1111
import { App } from 'antd';
1212
import { isLoginPath, isSessionExpiredPath } from '@/utils/is';
13-
import { PageEnum } from '@/enums';
13+
import { PageEnum, TOKEN_KEY } from '@/enums';
14+
import { Session } from '@/utils/storage';
1415

1516
const isDev = process.env.NODE_ENV === 'development';
1617

@@ -27,6 +28,19 @@ const goLogin = () => {
2728
});
2829
};
2930

31+
const goHome = () => {
32+
const query = queryString.parse(history.location.search);
33+
const { redirect } = query as { redirect: string };
34+
history.push({
35+
pathname: PageEnum.BASE_HOME,
36+
search:
37+
redirect &&
38+
queryString.stringify({
39+
redirect: redirect,
40+
}),
41+
});
42+
};
43+
3044
/**
3145
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
3246
* */
@@ -44,6 +58,9 @@ export async function getInitialState(): Promise<{
4458
const fetchUserInfo = async () => {
4559
return await queryCurrentUser().catch(() => undefined);
4660
};
61+
if (Session.get(TOKEN_KEY)) {
62+
goHome();
63+
}
4764
return {
4865
fetchUserInfo,
4966
currentUser: isLoginPath() ? undefined : await fetchUserInfo(),

src/components/RightContent/AvatarDropdown.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import React, { useCallback } from 'react';
99
import { flushSync } from 'react-dom';
1010
import HeaderDropdown from '../HeaderDropdown';
1111
import { Session } from '@/utils/storage';
12+
import { PageEnum } from '@/enums';
1213

1314
export type GlobalHeaderRightProps = {
1415
menu?: boolean;
@@ -27,14 +28,15 @@ export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, childre
2728
*/
2829
const loginOut = async () => {
2930
await outLogin();
31+
Session.clear();
3032
const { search, pathname } = window.location;
3133
const urlParams = new URL(window.location.href).searchParams;
3234
/** 此方法会跳转到 redirect 参数所在的位置 */
3335
const redirect = urlParams.get('redirect');
3436
// Note: There may be security issues, please note
35-
if (window.location.pathname !== '/login' && !redirect) {
37+
if (window.location.pathname !== PageEnum.BASE_LOGIN && !redirect) {
3638
history.replace({
37-
pathname: '/login',
39+
pathname: PageEnum.BASE_LOGIN,
3840
search: stringify({
3941
redirect: pathname + search,
4042
}),

src/enums/pageEnum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export enum PageEnum {
44
// session expired
55
SESSION_EXPIRED = '/session-expired',
66
// basic home path
7-
BASE_HOME = '/dashboard',
7+
BASE_HOME = '/welcome',
88
// error page path
99
ERROR_PAGE = '/exception',
1010
// error log page path

0 commit comments

Comments
 (0)