1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Config } from './config';
7 const REDIRECT_TO_KEY = 'redirectTo';
8 export const REDIRECT_TO_APPLY_TO_PATH = 'redirectToApplyToPath';
10 export const storeRedirects = () => {
12 const { location: { href }, localStorage } = window;
13 const applyToPath = href.indexOf(REDIRECT_TO_APPLY_TO_PATH) > -1;
15 if (href.indexOf(REDIRECT_TO_KEY) > -1) {
16 redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1];
19 if (localStorage && redirectUrl) {
20 localStorage.setItem(REDIRECT_TO_KEY, redirectUrl);
23 localStorage.setItem(REDIRECT_TO_APPLY_TO_PATH, 'true');
28 export const handleRedirects = (token: string, config: Config) => {
29 const { localStorage } = window;
30 const { keepWebServiceUrl } = config;
32 if (localStorage && localStorage.getItem(REDIRECT_TO_KEY)) {
33 const redirectUrl = localStorage.getItem(REDIRECT_TO_KEY);
34 localStorage.removeItem(REDIRECT_TO_KEY);
35 const applyToPath = localStorage.getItem(REDIRECT_TO_APPLY_TO_PATH);
38 if (applyToPath === 'true') {
39 localStorage.removeItem(REDIRECT_TO_APPLY_TO_PATH);
41 window.location.pathname = redirectUrl;
44 const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?";
45 window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`;