Skip to content

Commit 59c920f

Browse files
authored
Merge pull request #359 from contentstack/bugFix/cmg-117
fixed back button on login page
2 parents 59e4de0 + 7d00e98 commit 59c920f

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import { useEffect } from "react";
2-
import { useLocation, useNavigate } from "react-router";
2+
import { useNavigate } from "react-router-dom";
33

44
const usePreventBackNavigation = (): void => {
5-
const navigate = useNavigate();
6-
const location = useLocation();
7-
8-
useEffect(() => {
9-
// push a dummy state to the history stack
10-
window.history.pushState(null, "", window.location.href);
11-
12-
const handlePopState = () => {
13-
navigate(location.pathname, { replace: true });
14-
};
15-
16-
window.addEventListener("popstate", handlePopState);
17-
18-
return () => {
19-
window.removeEventListener("popstate", handlePopState);
20-
};
21-
}, [navigate, location]);
22-
};
5+
const navigate = useNavigate();
236

7+
useEffect(() => {
8+
const handleBackNavigation = (event: PopStateEvent) => {
9+
event.preventDefault();
10+
navigate(window.location.pathname, { replace: true });
11+
};
12+
13+
window.history.pushState(null, "", window.location.href);
14+
15+
window.addEventListener("popstate", handleBackNavigation);
16+
17+
return () => {
18+
window.removeEventListener("popstate", handleBackNavigation);
19+
};
20+
}, [navigate]);
21+
};
2422
export default usePreventBackNavigation;

ui/src/pages/Login/index.tsx

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { userSession, requestSMSToken } from '../../services/api/login.service';
3030

3131
// Interface
3232
import { IProps, IStates, defaultStates, User, UserRes, LoginType } from './login.interface';
33-
import usePreventBackNavigation from '../../hooks/usePreventBackNavigation';
33+
3434

3535
//Components
3636
import AccountPage from '../../components/AccountPage';
@@ -42,7 +42,6 @@ import { RootState } from '../../store';
4242
const Login: FC<IProps> = () => {
4343
const [data, setData] = useState<LoginType>({});
4444

45-
usePreventBackNavigation();
4645

4746
// ************* Fetch Login Data ************
4847
const fetchData = async () => {
@@ -70,7 +69,7 @@ const Login: FC<IProps> = () => {
7069
// ************* ALL States Here ************
7170
const [loginStates, setLoginStates] = useState<IStates>(defaultStates);
7271
const [isLoading, setIsLoading] = useState<boolean>(false)
73-
const [isBlock, setIsBlock] = useState(false);
72+
// const [isBlock, setIsBlock] = useState(false);
7473

7574
const navigate = useNavigate();
7675
const location = useLocation();
@@ -197,13 +196,13 @@ const Login: FC<IProps> = () => {
197196
}
198197
};
199198

200-
useEffect(()=>{
201-
if(region && loginStates?.tfa){
202-
setIsBlock(true);
199+
// useEffect(()=>{
200+
// if(region && loginStates?.tfa){
201+
// setIsBlock(true);
203202

204-
}
203+
// }
205204

206-
},[loginStates]);
205+
// },[loginStates]);
207206

208207
// Function for TFA validation
209208
const TFAValidation = (value: string): string | undefined => {
@@ -242,20 +241,43 @@ const Login: FC<IProps> = () => {
242241
};
243242
};
244243

245-
useEffect(()=>{
246-
const handlePopState = (event: PopStateEvent) => {
247-
event.preventDefault();
248-
window.history.pushState(null, '', window.location.href);
244+
// useEffect(()=>{
245+
// const handlePopState = (event: PopStateEvent) => {
246+
// event.preventDefault();
247+
// window.history.pushState(null, '', window.location.href);
248+
// };
249+
// if(isBlock){
250+
// window.history.pushState(null, '', window.location.href);
251+
// }
252+
// window.history.pushState(null, '', window.location.href);
253+
// window.addEventListener('popstate',handlePopState);
254+
// return () => {
255+
// window.removeEventListener('popstate', handlePopState);
256+
// };
257+
// },[isBlock]);
258+
259+
useEffect(() => {
260+
261+
const redirectUrl = loginStates?.tfa && region ? `/login?region=${region}` : "/region-login"
262+
263+
const handleBackButton = () => {
264+
// Redirect to an internal route
265+
navigate(redirectUrl, { replace: true });
249266
};
250-
if(isBlock){
251-
window.history.pushState(null, '', window.location.href);
252-
}
267+
253268
window.history.pushState(null, '', window.location.href);
254-
window.addEventListener('popstate',handlePopState);
269+
270+
const handlePopState = () => {
271+
handleBackButton();
272+
};
273+
274+
window.addEventListener('popstate', handlePopState);
275+
255276
return () => {
256277
window.removeEventListener('popstate', handlePopState);
257278
};
258-
},[isBlock]);
279+
}, [navigate, loginStates]);
280+
259281

260282

261283
return (

0 commit comments

Comments
 (0)