Add currentUser and isAuthenticated to authcontext
This commit is contained in:
@@ -2,7 +2,11 @@ import React from 'react';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { Nav, Icon, Dropdown, Popover, Whisper } from 'rsuite';
|
||||
|
||||
import { useAuth } from '../../helpers/authContext';
|
||||
|
||||
export default function MenuDropdown() {
|
||||
const { setIsAuthenticated } = useAuth();
|
||||
|
||||
const history = useHistory();
|
||||
const triggerRef = React.createRef();
|
||||
|
||||
@@ -12,8 +16,7 @@ export default function MenuDropdown() {
|
||||
}
|
||||
|
||||
function handleLogout() {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
setIsAuthenticated(false);
|
||||
history.push('/');
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,17 @@ import { Redirect, Route } from 'react-router-dom';
|
||||
import { useAuth } from '../helpers/authContext';
|
||||
|
||||
const PrivateRoute = ({ component: Component, ...rest }) => {
|
||||
const { authToken } = useAuth();
|
||||
const { isAuthenticated } = useAuth();
|
||||
|
||||
return (
|
||||
<Route
|
||||
{...rest}
|
||||
render={(props) =>
|
||||
authToken ? <Component {...props} /> : <Redirect to='/login' />
|
||||
isAuthenticated ? (
|
||||
<Component {...props} />
|
||||
) : (
|
||||
<Redirect to='/login' />
|
||||
)
|
||||
}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user