Login redirection (#712)

Co-authored-by: navaneeth <navaneethpk@outlook.com>
This commit is contained in:
Arpit 2021-09-08 18:39:30 +05:30 committed by GitHub
parent 980198c4fb
commit 9682a82d4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 3 deletions

View file

@ -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 });
},

View file

@ -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 <Redirect to={{ pathname: '/login', state: { from: props.location } }} />;
return <Redirect
to={{ pathname: '/login', search: `?redirectTo=${props.location.pathname}`, state: { from: props.location } }} />;
}
// authorised so return component

View file

@ -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}`);
}