Skip to content

Commit

Permalink
#638: Redirect to requested resource on login (#665)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7d63d83)
  • Loading branch information
dsuren1 committed Nov 30, 2023
1 parent edeeee5 commit c2219ca
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions js/epics/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

0 comments on commit c2219ca

Please sign in to comment.