Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12815 Direct links to AWX do not reroute the user after authentication #14399

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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