From 097d6903de5430f579bf42c499aaa6660e797e61 Mon Sep 17 00:00:00 2001 From: Stein Elgethun <stein.elgethun@usit.uio.no> Date: Tue, 9 Nov 2021 14:46:08 +0100 Subject: [PATCH] Redirect back to requested page from protectedRoutes Issue: GREG-89 --- .../src/components/protectedRoute/index.tsx | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/frontend/src/components/protectedRoute/index.tsx b/frontend/src/components/protectedRoute/index.tsx index c5e3fe8f..bc7cc48c 100644 --- a/frontend/src/components/protectedRoute/index.tsx +++ b/frontend/src/components/protectedRoute/index.tsx @@ -1,6 +1,7 @@ import React from 'react' -import { Route, Redirect, RouteProps } from 'react-router-dom' +import { Route, RouteProps, useLocation, useHistory } from 'react-router-dom' +import { setCookie } from 'utils' import { useUserContext } from 'contexts' interface IProtectedRoute extends RouteProps { @@ -9,17 +10,19 @@ interface IProtectedRoute extends RouteProps { function ProtectedRoute({ children, ...rest }: IProtectedRoute) { // Simple protected route component - // TODO: redirect back to the requested site after login const { user } = useUserContext() + + if ( !user.auth ) { + const location = useLocation() + const history = useHistory() + setCookie('redirect', location.pathname) + history.push('/') + } + return ( - <Route - {...rest} - /* eslint-disable arrow-body-style */ - render={() => { - return user.auth ? children : <Redirect to="/" /> - }} - /* eslint-enable arrow-body-style */ - /> + <Route {...rest}> + {children} + </Route> ) } -- GitLab