Skip to content
Snippets Groups Projects
Commit 097d6903 authored by Stein Elgethun's avatar Stein Elgethun
Browse files

Redirect back to requested page from protectedRoutes

Issue: GREG-89
parent f053f8ae
No related branches found
No related tags found
1 merge request!140Greg 89 login redirect
Pipeline #99867 passed
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>
)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment