From c2219cac3905824dbc1dd07a768044c3ec80dc49 Mon Sep 17 00:00:00 2001 From: Suren Date: Tue, 21 Nov 2023 14:23:35 +0530 Subject: [PATCH] #638: Redirect to requested resource on login (#665) (cherry picked from commit 7d63d83e353937e425fb32c6ae382323f90c2e4c) --- js/epics/login.js | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/js/epics/login.js b/js/epics/login.js index f278dace2..e96ee0045 100644 --- a/js/epics/login.js +++ b/js/epics/login.js @@ -7,21 +7,48 @@ */ import {Observable} from "rxjs"; import { LOGIN_REQUIRED } from '@mapstore/actions/security'; +import { LOCATION_CHANGE } from 'connected-react-router'; +import { isLoggedIn } from "@mapstore/selectors/security"; -const goToLoginPage = () => { - window.location.replace('/?login'); +const CAS_REDIRECT_PATH = 'casRedirectPath'; +const goToPage = (path) => { + window.location.replace(path); }; const redirectToLoginPage = (action$) => action$.ofType(LOGIN_REQUIRED) .switchMap(() => { - /* - Note: After login the user is not redirected back to the same resource requested, - as CAS login currently doesn't support that - */ - goToLoginPage(); + window.sessionStorage.setItem(CAS_REDIRECT_PATH, window.location.hash); + /* + Note: After login, the user is not redirected back to the previously requested resource as the CAS skips the hash part. + Hence, the side effect is performed by `casRedirectOnLogin` epic to redirect back to the same resource requested + */ + goToPage('/mapstore/?login'); + return Observable.empty(); + }); + +const casRedirectOnLogin = (action$, state) => + action$.ofType(LOCATION_CHANGE) + .filter(({payload} = {}) => payload?.location?.pathname === '/' && window.sessionStorage.getItem(CAS_REDIRECT_PATH)) + .switchMap(() => { + if (isLoggedIn(state.getState())) { + const redirectPath = window.sessionStorage.getItem(CAS_REDIRECT_PATH); + /* + Once the redirection is performed, redirect path is removed + */ + window.sessionStorage.removeItem(CAS_REDIRECT_PATH); + goToPage(redirectPath); + } else { + /* + Remove redirect path when user manually redirect and/or skips login. + i.e auto redirect is removed in this process to preclude any unforeseen redirections + */ + window.sessionStorage.removeItem(CAS_REDIRECT_PATH); + } + return Observable.empty(); }); export default { - redirectToLoginPage + redirectToLoginPage, + casRedirectOnLogin };