Skip to content

Commit

Permalink
Merge pull request #673 from dsuren1/#638_backport
Browse files Browse the repository at this point in the history
[Backport 2023.02.xx] #638: Redirect to requested resource on login (#665)
  • Loading branch information
tdipisa authored Nov 30, 2023
2 parents edeeee5 + c2219ca commit d2a59dc
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 d2a59dc

Please sign in to comment.