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

[Backport 2023.02.xx] #638: Redirect to requested resource on login (#665) #673

Merged
merged 1 commit into from
Nov 30, 2023
Merged
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
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
};
Loading