Skip to content
Snippets Groups Projects

Greg 89 login redirect

Merged elg requested to merge GREG-89-login-redirect into master
1 file
+ 13
10
Compare changes
  • Side-by-side
  • Inline
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>
)
}
Loading