Skip to content

Instantly share code, notes, and snippets.

@viniciusdacal
Created April 30, 2020 22:46
Show Gist options
  • Save viniciusdacal/798b123d2cf89216c67697e495e1aba9 to your computer and use it in GitHub Desktop.
Save viniciusdacal/798b123d2cf89216c67697e495e1aba9 to your computer and use it in GitHub Desktop.
import React, { useContext } from 'react';
import { Route, Redirect } from 'react-router-dom';
import StoreContext from 'components/Store/Context';
const RoutesPrivate = ({ component: Component, ...rest}) => {
const { token } = useContext(StoreContext);
return (
<Route
{...rest}
render={({ location }) => token
? <Component {...rest} />
: (
<Redirect
to={{
pathname: '/login',
state: {
from: location,
redirectOnAuthenticated: true,
},
}}
/>
)
}
/>
)
}
export default RoutesPrivate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment