Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(Router): Redirect to previous page after login or logout

haitao(lj)6f9250ead6e9
diff --git a/ui/src/components/Header/index.tsx b/ui/src/components/Header/index.tsx
index db052484..f98e3f4f 100644
--- a/ui/src/components/Header/index.tsx
+++ b/ui/src/components/Header/index.tsx
@@ -19,6 +19,7 @@ import {
import classnames from 'classnames';
+import { floppyNavigation } from '@/utils';
import {
loggedUserInfoStore,
siteInfoStore,
@@ -27,7 +28,6 @@ import {
themeSettingStore,
} from '@/stores';
import { logout, useQueryNotificationStatus } from '@/services';
-import { RouteAlias } from '@/router/alias';
import { DEFAULT_SITE_NAME } from '@/common/constants';
import NavItems from './components/NavItems';
@@ -36,7 +36,7 @@ import './index.scss';
const Header: FC = () => {
const navigate = useNavigate();
- const { user, clear } = loggedUserInfoStore();
+ const { user, clear: clearUserStore } = loggedUserInfoStore();
const { t } = useTranslation();
const [urlSearch] = useSearchParams();
const q = urlSearch.get('q');
@@ -60,8 +60,13 @@ const Header: FC = () => {
const handleLogout = async () => {
await logout();
- clear();
- navigate(RouteAlias.home);
+ clearUserStore();
+ };
+ const onLoginClick = (evt) => {
+ evt.preventDefault();
+ floppyNavigation.navigateToLogin((loginPath) => {
+ navigate(loginPath, { replace: true });
+ });
};
useEffect(() => {
@@ -132,6 +137,7 @@ const Header: FC = () => {
'link-light': navbarStyle === 'theme-colored',
'link-primary': navbarStyle !== 'theme-colored',
})}
+ onClick={onLoginClick}
href="/users/login">
{t('btns.login')}
</Button>
@@ -219,6 +225,7 @@ const Header: FC = () => {
'link-light': navbarStyle === 'theme-colored',
'link-primary': navbarStyle !== 'theme-colored',
})}
+ onClick={onLoginClick}
href="/users/login">
{t('btns.login')}
</Button>
diff --git a/ui/src/pages/Users/Login/index.tsx b/ui/src/pages/Users/Login/index.tsx
index 5634354f..ff8aec57 100644
--- a/ui/src/pages/Users/Login/index.tsx
+++ b/ui/src/pages/Users/Login/index.tsx
@@ -3,6 +3,8 @@ import { Container, Form, Button, Col } from 'react-bootstrap';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import { Trans, useTranslation } from 'react-i18next';
+import { RouteAlias } from '@/router/alias';
+import { REDIRECT_PATH_STORAGE_KEY } from '@/common/constants';
import { usePageTags } from '@/hooks';
import type {
LoginReqParams,
@@ -13,8 +15,6 @@ import { Unactivate } from '@/components';
import { loggedUserInfoStore, loginSettingStore } from '@/stores';
import { guard, floppyNavigation, handleFormError } from '@/utils';
import { login, checkImgCode } from '@/services';
-import { REDIRECT_PATH_STORAGE_KEY } from '@/common/constants';
-import { RouteAlias } from '@/router/alias';
import { PicAuthCodeModal } from '@/components/Modal';
import Storage from '@/utils/storage';
@@ -91,6 +91,14 @@ const Index: React.FC = () => {
return bol;
};
+ const handleLoginRedirect = () => {
+ const redirect = Storage.get(REDIRECT_PATH_STORAGE_KEY) || RouteAlias.home;
+ Storage.remove(REDIRECT_PATH_STORAGE_KEY);
+ floppyNavigation.navigate(redirect, () => {
+ navigate(redirect, { replace: true });
+ });
+ };
+
const handleLogin = (event?: any) => {
if (event) {
event.preventDefault();
@@ -113,24 +121,12 @@ const Index: React.FC = () => {
setStep(2);
setRefresh((pre) => pre + 1);
} else {
- const path =
- Storage.get(REDIRECT_PATH_STORAGE_KEY) || RouteAlias.home;
- Storage.remove(REDIRECT_PATH_STORAGE_KEY);
- floppyNavigation.navigate(path, () => {
- navigate(path, { replace: true });
- });
+ handleLoginRedirect();
}
setModalState(false);
})
.catch((err) => {
- // if (err.isError && err.key) {
- // formData[err.key].isInvalid = true;
- // formData[err.key].errorMsg = err.value;
- // if (err.key.indexOf('captcha') < 0) {
- // setModalState(false);
- // }
- // }
if (err.isError) {
const data = handleFormError(err, formData);
if (!err.list.find((v) => v.error_field.indexOf('captcha') >= 0)) {
diff --git a/ui/src/router/RouteGuard.tsx b/ui/src/router/RouteGuard.tsx
index 3f4a17e1..0ec79272 100644
--- a/ui/src/router/RouteGuard.tsx
+++ b/ui/src/router/RouteGuard.tsx
@@ -3,6 +3,7 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { floppyNavigation } from '@/utils';
import { TGuardFunc } from '@/utils/guard';
+import { loggedUserInfoStore } from '@/stores';
const Index: FC<{
children: ReactNode;
@@ -16,7 +17,8 @@ const Index: FC<{
}) => {
const navigate = useNavigate();
const location = useLocation();
- useEffect(() => {
+ const { user } = loggedUserInfoStore();
+ const runGuards = () => {
if (onEnter) {
const gr = onEnter();
const redirectUrl = gr.redirect;
@@ -26,8 +28,15 @@ const Index: FC<{
});
}
}
+ };
+ useEffect(() => {
+ runGuards();
}, [location]);
-
+ useEffect(() => {
+ if (!user.access_token) {
+ runGuards();
+ }
+ }, [user]);
return (
<>
{/* Route Guard */}
diff --git a/ui/src/utils/floppyNavigation.ts b/ui/src/utils/floppyNavigation.ts
index 2fd6f565..41d8a9ac 100644
--- a/ui/src/utils/floppyNavigation.ts
+++ b/ui/src/utils/floppyNavigation.ts
@@ -33,10 +33,13 @@ const navigate = (pathname: string, callback: Function) => {
/**
* auto navigate to login page with redirect info
*/
-const navigateToLogin = () => {
- storageLoginRedirect();
+const navigateToLogin = (callback?: Function) => {
navigate(RouteAlias.login, () => {
- window.location.replace(RouteAlias.login);
+ if (callback) {
+ callback(RouteAlias.login);
+ } else {
+ window.location.replace(RouteAlias.login);
+ }
});
};