From cf8c910d220a4432021e43b82ded2b8e5e2cfba0 Mon Sep 17 00:00:00 2001 From: Ryan Berger Date: Mon, 9 Sep 2024 15:13:50 -0400 Subject: [PATCH] [STCOR-885] Clear saved entry path so that subsequent logins will use default base URL. (#1531) * Clear saved entry path so that subsequent logins will use default base URL * Moving removeUnauthorizedPathFromSession() to OIDCRedirect so the value is cleared right after being used rather than on logout * Add comment (cherry picked from commit f6948219696cfb049cc313f9c229c08325eeb29e) --- src/components/AuthnLogin/AuthnLogin.js | 2 +- src/components/OIDCRedirect.js | 12 +++++++++--- src/loginServices.js | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/AuthnLogin/AuthnLogin.js b/src/components/AuthnLogin/AuthnLogin.js index 093c3db68..6c11bd726 100644 --- a/src/components/AuthnLogin/AuthnLogin.js +++ b/src/components/AuthnLogin/AuthnLogin.js @@ -36,7 +36,7 @@ const AuthnLogin = ({ stripes }) => { * @see OIDCRedirect */ if (okapi.authnUrl && window.location.pathname !== '/') { - setUnauthorizedPathToSession(window.location.pathname); + setUnauthorizedPathToSession(); } // If only 1 tenant is defined in config (in either okapi or config.tenantOptions) set to okapi to be accessed there diff --git a/src/components/OIDCRedirect.js b/src/components/OIDCRedirect.js index c224b3dad..9d463fe9a 100644 --- a/src/components/OIDCRedirect.js +++ b/src/components/OIDCRedirect.js @@ -1,7 +1,11 @@ import { withRouter, Redirect, useLocation } from 'react-router'; import queryString from 'query-string'; import { useStripes } from '../StripesContext'; -import { getUnauthorizedPathFromSession } from '../loginServices'; +import { getUnauthorizedPathFromSession, removeUnauthorizedPathFromSession } from '../loginServices'; + +// Setting at top of component since value should be retained during re-renders +// but will be correctly re-fetched when redirected from Keycloak login page. +const unauthorizedPath = getUnauthorizedPathFromSession(); /** * OIDCRedirect authenticated route handler for /oidc-landing. @@ -29,8 +33,10 @@ const OIDCRedirect = () => { const getUrl = () => { if (stripes.okapi.authnUrl) { - const unauthorizedPath = getUnauthorizedPathFromSession(); - if (unauthorizedPath) return unauthorizedPath; + if (unauthorizedPath) { + removeUnauthorizedPathFromSession(); + return unauthorizedPath; + } } const params = getParams(); diff --git a/src/loginServices.js b/src/loginServices.js index 01f1167b1..1e39e20de 100644 --- a/src/loginServices.js +++ b/src/loginServices.js @@ -124,7 +124,7 @@ export const removeUnauthorizedPathFromSession = () => sessionStorage.removeItem export const setUnauthorizedPathToSession = (pathname) => { const path = pathname ?? `${window.location.pathname}${window.location.search}`; if (!path.startsWith('/logout')) { - sessionStorage.setItem(UNAUTHORIZED_PATH, pathname ?? `${window.location.pathname}${window.location.search}`); + sessionStorage.setItem(UNAUTHORIZED_PATH, path); } }; export const getUnauthorizedPathFromSession = () => sessionStorage.getItem(UNAUTHORIZED_PATH);