Skip to content
Snippets Groups Projects
Commit cae31b08 authored by Embruch, Gerd's avatar Embruch, Gerd
Browse files

fixed login & added logout

parent 5a8c0489
No related branches found
No related tags found
No related merge requests found
......@@ -31,20 +31,20 @@ function AuthState({ children }) {
credentials,
{ withCredentials: true }
);
console.log;
// set current user to login and merge accessToken into currentUser
dispatchCurrentUser({ type: USER_ACTIONS.SET, payload: { ...result.data.document } });
setAccessToken(result.data.accessToken);
dispatchCurrentUser({ type: USER_ACTIONS.SET, payload: { ...result.data.record } });
setAccessToken(result.data.token);
// TODO: don't store accessToken in localStorage, keep in memory only
localStorage.setItem("accessToken", JSON.stringify(result.data.accessToken));
localStorage.setItem("accessToken", JSON.stringify(result.data.token));
return result;
}
// ### HANDLE LOGOUT
async function logout() {
dispatchCurrentUser({ type: USER_ACTIONS.DROP });
const result = await api.delete(
const result = await api.get(
'/users/logout',
{ withCredentials: true }
);
return result;
}
......
......@@ -14,6 +14,7 @@ function Navbar(props) {
useEffect(() => {
// fetch all links for navbars
const [overall] = sitemap.filter((item) => item.title === 'MenuBar');
// fetch all children from /
const [home] = overall.children;
......
import React, { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '../../contexts/Auth/AuthState';
import { mergeBackendValidation, setFlashMsg } from '../../utils/ErrorHandling';
function Logout() {
// #################################
// HOOKS
// #################################
// ### CONNECT AUTH CONTEXT
const { logout } = useAuth();
// ### LOGOUT IMMEDIATLY AFTER LOADING
useEffect(() => {
handleLogout();
}, []);
// ### ENABLE Redirections
const redirect = useNavigate();
// #################################
// FUNCTIONS
// #################################
async function handleLogout() {
try {
const result = await logout();
redirect('/login');
// set flash mmessagesg
setFlashMsg(result.data?.message);
} catch (err) {
console.error(err);
mergeBackendValidation(500, err);
}
}
// #################################
// OUTPUT
// #################################
return (
<div>You're about to log out</div>
);
}
export default React.memo(Logout);
\ No newline at end of file
......@@ -6,8 +6,8 @@ import CleanLayout from '../layouts/CleanLayout';
export const sitemap = [{
title: 'MenuBar', element: <MainLayout />,
children: [
{
children: [{
// HOME
title: 'Home',
path: '/',
handle: { crumb: () => <Link to="/">ZBH Portal</Link> },
......@@ -22,10 +22,11 @@ export const sitemap = [{
path: '/onboarding',
element: loadComponent('Onboarding/Onboarding', true, true),
handle: { crumb: () => <Link to="/onboarding">Onboarding</Link> }
}
]
}
},
// LOGOUT
{ title: 'Logout', path: '/logout', element: loadComponent('User/Logout', true, true), handle: { crumb: () => <Link to="/profile/logout">Logout</Link> } }
]
}]
}, {
title: 'Others', element: <CleanLayout />, children: [
// USER
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment