From c2147f46a008d5e5d32c643c96783c668e94f322 Mon Sep 17 00:00:00 2001 From: Alexey Kontsevoy Date: Fri, 5 Jul 2019 11:34:39 -0400 Subject: [PATCH] Handle redirects on login page --- web/src/cluster/components/Logs/Logs.jsx | 2 +- web/src/components/Login/LoginFailed.jsx | 2 +- web/src/flux/user/actions.js | 23 +++++++++++++++-------- web/src/services/browser.js | 17 +---------------- web/src/services/history.js | 23 ++++++++++++++++------- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/web/src/cluster/components/Logs/Logs.jsx b/web/src/cluster/components/Logs/Logs.jsx index f73fccd394..6a28b1ac92 100644 --- a/web/src/cluster/components/Logs/Logs.jsx +++ b/web/src/cluster/components/Logs/Logs.jsx @@ -20,7 +20,7 @@ import cfg from 'app/config'; import LogViewer from 'app/components/LogViewer'; import LogProvider from './LogProvider'; import QueryEditorBasic from './QueryEditorBasic'; -import { getUrlParameter } from 'app/services/browser'; +import { getUrlParameter } from 'app/services/history'; import * as Alerts from 'shared/components/Alert'; import { Box, Flex, Input, ButtonSecondary } from 'shared/components'; import { Cog } from 'shared/components/Icon'; diff --git a/web/src/components/Login/LoginFailed.jsx b/web/src/components/Login/LoginFailed.jsx index 1015f6de2d..406f273470 100644 --- a/web/src/components/Login/LoginFailed.jsx +++ b/web/src/components/Login/LoginFailed.jsx @@ -15,7 +15,7 @@ limitations under the License. */ import React from 'react'; -import { getUrlParameter } from 'app/services/browser'; +import { getUrlParameter } from 'app/services/history'; import cfg from 'app/config'; import LogoHero from './../LogoHero'; import { LoginFailed as CardFailed} from 'shared/components/CardError'; diff --git a/web/src/flux/user/actions.js b/web/src/flux/user/actions.js index e0441d2100..a5cf3ddeb5 100644 --- a/web/src/flux/user/actions.js +++ b/web/src/flux/user/actions.js @@ -27,12 +27,12 @@ const logger = Logger.create('user/actions'); export function login(userId, password, token) { const promise = auth.login(userId, password, token); - return _handleLoginPromise(promise); + return handleLoginPromise(promise); } export function loginWithU2f(userId, password) { const promise = auth.loginWithU2f(userId, password); - return _handleLoginPromise(promise); + return handleLoginPromise(promise); } export function loginWithSso(providerName, redirectUrl) { @@ -59,12 +59,7 @@ export function changePassword(oldPsw, newPsw, token){ return api.put(cfg.getSiteChangePasswordUrl(), data); } -function getEntryRoute() { - const entryUrl = history.getRedirectParam(); - return !entryUrl ? cfg.routes.app : entryUrl; -} - -function _handleLoginPromise(promise) { +function handleLoginPromise(promise) { return promise.done(() => { const redirect = getEntryRoute(); const withPageRefresh = true; @@ -73,4 +68,16 @@ function _handleLoginPromise(promise) { .fail(err => { logger.error('login', err); }); +} + +function getEntryRoute() { + let entryUrl = history.getRedirectParam(); + if (entryUrl) { + entryUrl = history.ensureKnownRoute(entryUrl); + } else { + entryUrl = cfg.routes.app; + } + + return history.ensureBaseUrl(entryUrl); + } \ No newline at end of file diff --git a/web/src/services/browser.js b/web/src/services/browser.js index d249d9cde6..fd7fa8984f 100644 --- a/web/src/services/browser.js +++ b/web/src/services/browser.js @@ -23,19 +23,4 @@ function detectPlatform() { } } -export function getUrlParameter(name, path) { - path = path || window.location.search; - const query = path.substring(1); - const vars = query.split('&'); - for (var i = 0; i < vars.length; i++) { - var pair = vars[i].split('='); - if (decodeURIComponent(pair[0]) == name) { - return decodeURIComponent(pair[1]); - } - } - - return ''; -} - -export const platform = detectPlatform(); - +export const platform = detectPlatform(); \ No newline at end of file diff --git a/web/src/services/history.js b/web/src/services/history.js index a96eaef73c..1d446e3070 100644 --- a/web/src/services/history.js +++ b/web/src/services/history.js @@ -62,12 +62,7 @@ const history = { }, getRedirectParam() { - let loc = this.original().location; - if (loc.query && loc.query.redirect_uri) { - return loc.query.redirect_uri; - } - - return ''; + return getUrlParameter('redirect_uri', this.original().location.search); }, ensureKnownRoute(url) { @@ -117,4 +112,18 @@ const match = url => route => { }) } -export default history; \ No newline at end of file +export default history; + +export function getUrlParameter(name, path) { + path = path || window.location.search; + const query = path.substring(1); + const vars = query.split('&'); + for (var i = 0; i < vars.length; i++) { + var pair = vars[i].split('='); + if (decodeURIComponent(pair[0]) == name) { + return decodeURIComponent(pair[1]); + } + } + + return ''; +} \ No newline at end of file