From 9d60b0b9c6c78bc866990afbcae05d6e93989d6d Mon Sep 17 00:00:00 2001 From: Sasa Jovicic Date: Tue, 3 Oct 2023 22:55:22 +0200 Subject: [PATCH] Fix #12815 Direct links to AWX do not reroute the user after authentication (#14399) Signed-off-by: Sasa993 Co-authored-by: Sasa Jovicic --- awx/ui/src/contexts/Session.js | 18 ++++++++++++++++++ awx/ui/src/screens/Login/Login.js | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/awx/ui/src/contexts/Session.js b/awx/ui/src/contexts/Session.js index af34948313..d0a92d7523 100644 --- a/awx/ui/src/contexts/Session.js +++ b/awx/ui/src/contexts/Session.js @@ -75,6 +75,7 @@ function SessionProvider({ children }) { const [sessionCountdown, setSessionCountdown] = useState(0); const [authRedirectTo, setAuthRedirectTo] = useState('/'); const [isUserBeingLoggedOut, setIsUserBeingLoggedOut] = useState(false); + const [isRedirectLinkReceived, setIsRedirectLinkReceived] = useState(false); const { request: fetchLoginRedirectOverride, @@ -99,6 +100,7 @@ function SessionProvider({ children }) { const logout = useCallback(async () => { setIsUserBeingLoggedOut(true); + setIsRedirectLinkReceived(false); if (!isSessionExpired.current) { setAuthRedirectTo('/logout'); window.localStorage.setItem(SESSION_USER_ID, null); @@ -112,6 +114,18 @@ function SessionProvider({ children }) { return ; }, [setSessionTimeout, setSessionCountdown]); + useEffect(() => { + const unlisten = history.listen((location, action) => { + if (action === 'POP') { + setIsRedirectLinkReceived(true); + } + }); + + return () => { + unlisten(); // ensure that the listener is removed when the component unmounts + }; + }, [history]); + useEffect(() => { if (!isAuthenticated(document.cookie)) { return () => {}; @@ -176,6 +190,8 @@ function SessionProvider({ children }) { logout, sessionCountdown, setAuthRedirectTo, + isRedirectLinkReceived, + setIsRedirectLinkReceived, }), [ authRedirectTo, @@ -186,6 +202,8 @@ function SessionProvider({ children }) { logout, sessionCountdown, setAuthRedirectTo, + isRedirectLinkReceived, + setIsRedirectLinkReceived, ] ); diff --git a/awx/ui/src/screens/Login/Login.js b/awx/ui/src/screens/Login/Login.js index 0e9009a10a..6abfa86b61 100644 --- a/awx/ui/src/screens/Login/Login.js +++ b/awx/ui/src/screens/Login/Login.js @@ -45,7 +45,8 @@ const Login = styled(PFLogin)` function AWXLogin({ alt, isAuthenticated }) { const [userId, setUserId] = useState(null); - const { authRedirectTo, isSessionExpired } = useSession(); + const { authRedirectTo, isSessionExpired, isRedirectLinkReceived } = + useSession(); const isNewUser = useRef(true); const hasVerifiedUser = useRef(false); @@ -179,7 +180,8 @@ function AWXLogin({ alt, isAuthenticated }) { return ; } if (userId && hasVerifiedUser.current) { - const redirect = isNewUser.current ? '/home' : authRedirectTo; + const redirect = + isNewUser.current && !isRedirectLinkReceived ? '/home' : authRedirectTo; return ; }