Skip to content

Commit

Permalink
Fix ansible#12815 Direct links to AWX do not reroute the user after a…
Browse files Browse the repository at this point in the history
…uthentication (ansible#14399)

Signed-off-by: Sasa993 <jovicic.sasa@hotmail.com>
Co-authored-by: Sasa Jovicic <sjovicic@anexia-it.com>
  • Loading branch information
2 people authored and djyasin committed Nov 11, 2024
1 parent e68ed1b commit 3ded481
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 18 additions & 0 deletions awx/ui/src/contexts/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -112,6 +114,18 @@ function SessionProvider({ children }) {
return <Redirect to="/login" />;
}, [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 () => {};
Expand Down Expand Up @@ -176,6 +190,8 @@ function SessionProvider({ children }) {
logout,
sessionCountdown,
setAuthRedirectTo,
isRedirectLinkReceived,
setIsRedirectLinkReceived,
}),
[
authRedirectTo,
Expand All @@ -186,6 +202,8 @@ function SessionProvider({ children }) {
logout,
sessionCountdown,
setAuthRedirectTo,
isRedirectLinkReceived,
setIsRedirectLinkReceived,
]
);

Expand Down
6 changes: 4 additions & 2 deletions awx/ui/src/screens/Login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -179,7 +180,8 @@ function AWXLogin({ alt, isAuthenticated }) {
return <LoadingSpinner />;
}
if (userId && hasVerifiedUser.current) {
const redirect = isNewUser.current ? '/home' : authRedirectTo;
const redirect =
isNewUser.current && !isRedirectLinkReceived ? '/home' : authRedirectTo;

return <Redirect to={redirect} />;
}
Expand Down

0 comments on commit 3ded481

Please sign in to comment.