17337: Added another edge case handling
[arvados-workbench2.git] / src / common / redirect-to.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Config } from './config';
6
7 const REDIRECT_TO_KEY = 'redirectTo';
8
9 export const storeRedirects = () => {
10     let redirectUrl;
11     const { location: { href }, localStorage } = window;
12
13     if (href.indexOf(REDIRECT_TO_KEY) > -1) {
14         redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1];
15     }
16
17     if (localStorage && redirectUrl) {
18         localStorage.setItem(REDIRECT_TO_KEY, redirectUrl);
19     }
20 };
21
22 export const handleRedirects = (token: string, config: Config) => {
23     const { localStorage } = window;
24     const { keepWebServiceUrl } = config;
25
26     if (localStorage && localStorage.getItem(REDIRECT_TO_KEY)) {
27         const redirectUrl = localStorage.getItem(REDIRECT_TO_KEY);
28         localStorage.removeItem(REDIRECT_TO_KEY);
29
30         if (redirectUrl) {
31             const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?";
32             window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`;
33         }
34     }
35 };