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 }) { ...@@ -31,20 +31,20 @@ function AuthState({ children }) {
credentials, credentials,
{ withCredentials: true } { withCredentials: true }
); );
console.log;
// set current user to login and merge accessToken into currentUser // set current user to login and merge accessToken into currentUser
dispatchCurrentUser({ type: USER_ACTIONS.SET, payload: { ...result.data.document } }); dispatchCurrentUser({ type: USER_ACTIONS.SET, payload: { ...result.data.record } });
setAccessToken(result.data.accessToken); setAccessToken(result.data.token);
// TODO: don't store accessToken in localStorage, keep in memory only // 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; return result;
} }
// ### HANDLE LOGOUT // ### HANDLE LOGOUT
async function logout() { async function logout() {
dispatchCurrentUser({ type: USER_ACTIONS.DROP }); dispatchCurrentUser({ type: USER_ACTIONS.DROP });
const result = await api.delete( const result = await api.get(
'/users/logout', '/users/logout',
{ withCredentials: true }
); );
return result; return result;
} }
......
...@@ -14,6 +14,7 @@ function Navbar(props) { ...@@ -14,6 +14,7 @@ function Navbar(props) {
useEffect(() => { useEffect(() => {
// fetch all links for navbars // fetch all links for navbars
const [overall] = sitemap.filter((item) => item.title === 'MenuBar'); const [overall] = sitemap.filter((item) => item.title === 'MenuBar');
// fetch all children from / // fetch all children from /
const [home] = overall.children; 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'; ...@@ -6,8 +6,8 @@ import CleanLayout from '../layouts/CleanLayout';
export const sitemap = [{ export const sitemap = [{
title: 'MenuBar', element: <MainLayout />, title: 'MenuBar', element: <MainLayout />,
children: [ children: [{
{ // HOME
title: 'Home', title: 'Home',
path: '/', path: '/',
handle: { crumb: () => <Link to="/">ZBH Portal</Link> }, handle: { crumb: () => <Link to="/">ZBH Portal</Link> },
...@@ -22,10 +22,11 @@ export const sitemap = [{ ...@@ -22,10 +22,11 @@ export const sitemap = [{
path: '/onboarding', path: '/onboarding',
element: loadComponent('Onboarding/Onboarding', true, true), element: loadComponent('Onboarding/Onboarding', true, true),
handle: { crumb: () => <Link to="/onboarding">Onboarding</Link> } 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: [ title: 'Others', element: <CleanLayout />, children: [
// USER // USER
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment