From b360724baa222d0f2c93ff2e375f012fdd61fdc2 Mon Sep 17 00:00:00 2001 From: Philipp Grosswiler <88877786+grosswilerp@users.noreply.github.com> Date: Fri, 3 Sep 2021 08:55:34 +0200 Subject: [PATCH] feat(auth): added support for redirecting to initial page after login (#797) close #719 --- client/src/components/Root/Root.jsx | 16 +++++++++++----- client/src/containers/Login/Login.jsx | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/client/src/components/Root/Root.jsx b/client/src/components/Root/Root.jsx index d2e0bed4f..e9e70c89d 100644 --- a/client/src/components/Root/Root.jsx +++ b/client/src/components/Root/Root.jsx @@ -1,4 +1,4 @@ -import {Component} from 'react'; +import { Component } from 'react'; import axios from 'axios'; import { get, put, post, remove } from '../../utils/api'; @@ -7,6 +7,12 @@ class Root extends Component { cancel = axios.CancelToken.source(); componentWillUnmount() { + const pathname = window.location.pathname; + + if (pathname !== '/ui/login') { + sessionStorage.setItem('returnTo', pathname + (window.location.search || '')); + } + this.cancelAxiosRequests(); } @@ -21,16 +27,16 @@ class Root extends Component { } getApi(url) { - return get(url, {cancelToken: this.cancel.token}) + return get(url, { cancelToken: this.cancel.token }) } postApi(url, body) { - return post(url, body,{cancelToken: this.cancel.token}) + return post(url, body, { cancelToken: this.cancel.token }) } putApi(url, body) { - return put(url, body,{cancelToken: this.cancel.token}) + return put(url, body, { cancelToken: this.cancel.token }) } removeApi(url, body) { - return remove(url, body, {cancelToken: this.cancel.token}) + return remove(url, body, { cancelToken: this.cancel.token }) } } diff --git a/client/src/containers/Login/Login.jsx b/client/src/containers/Login/Login.jsx index 7bbc3759f..e4579c65c 100644 --- a/client/src/containers/Login/Login.jsx +++ b/client/src/containers/Login/Login.jsx @@ -1,7 +1,7 @@ import React from 'react'; import logo from '../../images/logo.svg'; -import {uriCurrentUser, uriLogin, uriOidc} from '../../utils/endpoints'; +import { uriCurrentUser, uriLogin, uriOidc } from '../../utils/endpoints'; import { organizeRoles } from '../../utils/converters'; import { login } from '../../utils/api'; import Form from '../../components/Form/Form'; @@ -59,9 +59,13 @@ class Login extends Form { sessionStorage.setItem('user', currentUserData.username); sessionStorage.setItem('roles', organizeRoles(currentUserData.roles)); + const returnTo = sessionStorage.getItem('returnTo'); + sessionStorage.removeItem('returnTo'); + this.props.history.push({ - pathname: '/ui', + pathname: (returnTo || '/ui'), }); + window.location.reload(true); } else { toast.error('Wrong Username or Password!'); @@ -70,9 +74,9 @@ class Login extends Form { componentDidMount() { const auths = JSON.parse(sessionStorage.getItem('auths')); - if(auths && auths.loginEnabled) { - const {loginEnabled, ...config} = auths; - this.setState({config}); + if (auths && auths.loginEnabled) { + const { loginEnabled, ...config } = auths; + this.setState({ config }); } else { this.props.history.push({ pathname: '/ui', @@ -86,9 +90,9 @@ class Login extends Form { <>
- - - + + +