From bcbfd3e38c50d967eb0b586d5c14afaace2e842d Mon Sep 17 00:00:00 2001 From: Youssef Date: Mon, 21 Feb 2022 12:33:11 +0100 Subject: [PATCH] feat: add redirect params to /login --- frontend/src/component/App.tsx | 2 -- .../component/common/ProtectedRoute/ProtectedRoute.jsx | 8 ++++++-- frontend/src/component/user/Login/Login.tsx | 8 ++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/frontend/src/component/App.tsx b/frontend/src/component/App.tsx index d2293e5a30..56cd5c8625 100644 --- a/frontend/src/component/App.tsx +++ b/frontend/src/component/App.tsx @@ -41,7 +41,6 @@ export const App = () => { const renderRoute = (route: any) => { if (route.type === 'protected') { const unauthorized = isUnauthorized(); - return ( { path="/" unauthorized={isUnauthorized()} component={Redirect} - renderProps={{ to: '/features' }} /> {renderMainLayoutRoutes()} {renderStandaloneRoutes()} diff --git a/frontend/src/component/common/ProtectedRoute/ProtectedRoute.jsx b/frontend/src/component/common/ProtectedRoute/ProtectedRoute.jsx index 710626db9e..7c26fda618 100644 --- a/frontend/src/component/common/ProtectedRoute/ProtectedRoute.jsx +++ b/frontend/src/component/common/ProtectedRoute/ProtectedRoute.jsx @@ -1,4 +1,4 @@ -import { Route, Redirect } from 'react-router-dom'; +import { Route, useLocation, useHistory } from 'react-router-dom'; const ProtectedRoute = ({ component: Component, @@ -6,12 +6,16 @@ const ProtectedRoute = ({ renderProps = {}, ...rest }) => { + const { pathname } = useLocation(); + const history = useHistory(); + const loginLink = + pathname.length > 1 ? `/login?redirect=${pathname}` : '/login'; return ( { if (unauthorized) { - return ; + history.push(loginLink); } else { return ; } diff --git a/frontend/src/component/user/Login/Login.tsx b/frontend/src/component/user/Login/Login.tsx index 4e7f266710..efa57810f3 100644 --- a/frontend/src/component/user/Login/Login.tsx +++ b/frontend/src/component/user/Login/Login.tsx @@ -7,7 +7,7 @@ import { DEMO_TYPE } from '../../../constants/authTypes'; import Authentication from '../Authentication/Authentication'; import { useAuthDetails } from '../../../hooks/api/getters/useAuth/useAuthDetails'; import { useAuthUser } from '../../../hooks/api/getters/useAuth/useAuthUser'; -import { Redirect } from 'react-router-dom'; +import { useHistory } from 'react-router-dom'; const Login = () => { const styles = useStyles(); @@ -15,9 +15,13 @@ const Login = () => { const { user } = useAuthUser(); const query = useQueryParams(); const resetPassword = query.get('reset') === 'true'; + const redirect = query.get('redirect') || '/features'; + const history = useHistory(); + console.log(redirect); if (user) { - return ; + console.log(redirect); + history.push(redirect); } return (