From 9682a82d4b45112cee5a51e9b90ed428939d5e49 Mon Sep 17 00:00:00 2001 From: Arpit Date: Wed, 8 Sep 2021 18:39:30 +0530 Subject: [PATCH] Login redirection (#712) Co-authored-by: navaneeth --- frontend/src/LoginPage/LoginPage.jsx | 4 +++- frontend/src/_components/PrivateRoute.jsx | 3 ++- frontend/src/_services/authentication.service.js | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/src/LoginPage/LoginPage.jsx b/frontend/src/LoginPage/LoginPage.jsx index e0a841df8f..d6b9694a8b 100644 --- a/frontend/src/LoginPage/LoginPage.jsx +++ b/frontend/src/LoginPage/LoginPage.jsx @@ -2,6 +2,7 @@ import React from 'react'; import { authenticationService } from '@/_services'; import { toast } from 'react-toastify'; import { Link } from 'react-router-dom'; +import queryString from 'query-string'; class LoginPage extends React.Component { constructor(props) { @@ -30,7 +31,8 @@ class LoginPage extends React.Component { authenticationService.login(email, password).then( () => { - const { from } = this.props.location.state || { from: { pathname: '/' } }; + const params = queryString.parse(this.props.location.search) + const { from } = params.redirectTo ? { from: { pathname: params.redirectTo } } : { from: { pathname: '/' } }; this.props.history.push(from); this.setState({ isLoading: false }); }, diff --git a/frontend/src/_components/PrivateRoute.jsx b/frontend/src/_components/PrivateRoute.jsx index 9f06b30d1d..e34fcf6d1d 100644 --- a/frontend/src/_components/PrivateRoute.jsx +++ b/frontend/src/_components/PrivateRoute.jsx @@ -10,7 +10,8 @@ export const PrivateRoute = ({ component: Component, switchDarkMode, darkMode, . const currentUser = authenticationService.currentUserValue; if (!currentUser && !props.location.pathname.startsWith('/applications/')) { // not logged in so redirect to login page with the return url - return ; + return ; } // authorised so return component diff --git a/frontend/src/_services/authentication.service.js b/frontend/src/_services/authentication.service.js index 5d2b5dcaab..4d1c10c5b2 100644 --- a/frontend/src/_services/authentication.service.js +++ b/frontend/src/_services/authentication.service.js @@ -57,5 +57,5 @@ function logout() { // remove user from local storage to log user out localStorage.removeItem('currentUser'); currentUserSubject.next(null); - history.push('/login'); + history.push(`/login?redirectTo=${window.location.pathname}`); }