diff --git a/frontend/src/components/protectedRoute/index.tsx b/frontend/src/components/protectedRoute/index.tsx index c5e3fe8f388c1a503a16ea692ddebba1dea3e4b5..bc7cc48c4abbd804d6e0c16cf4e9d7d2630cb64c 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> ) }