Add currentUser and isAuthenticated to authcontext

This commit is contained in:
2020-08-22 18:33:42 +02:00
parent a52bc8f7ab
commit 3356c380d7
7 changed files with 72 additions and 15 deletions

View File

@@ -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('/');
}

View File

@@ -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' />
)
}
/>
);