Implement logout handling when session is missing

This commit is contained in:
Neeraj 2024-10-17 15:20:52 +05:30 committed by Nabarun
parent 249e1079c4
commit 76e422ac92
3 changed files with 16 additions and 5 deletions

View File

@ -108,9 +108,12 @@ router.get('/session', (req, res) => {
});
router.post('/logout', (req, res) => {
// This is how you clear cookie-session
(req as any).session = null;
res.send({ success: true });
req.session.destroy((err) => {
if (err) {
return res.send({ success: false });
}
res.send({ success: true });
});
});
export default router;

View File

@ -59,12 +59,17 @@ function App() {
fetch(`${baseUrl}/auth/session`, {
credentials: 'include',
}).then((res) => {
const path = window.location.pathname;
if (res.status !== 200) {
localStorage.clear();
const path = window.location.pathname;
if (path !== '/login') {
window.location.pathname = '/login';
}
} else {
if (path === '/login') {
window.location.pathname = '/';
}
}
});
}, []);

View File

@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { NavLink, useNavigate, useParams } from 'react-router-dom';
import { Organization, User } from 'gql-client';
import { motion } from 'framer-motion';
import { useDisconnect } from 'wagmi';
import { useGQLClient } from 'context/GQLClientContext';
import {
@ -33,6 +34,7 @@ export const Sidebar = ({ mobileOpen }: SidebarProps) => {
const isDesktop = useMediaQuery('(min-width: 960px)');
const [user, setUser] = useState<User>();
const { disconnect } = useDisconnect();
const fetchUser = useCallback(async () => {
const { user } = await client.getUser();
@ -89,8 +91,9 @@ export const Sidebar = ({ mobileOpen }: SidebarProps) => {
credentials: 'include',
});
localStorage.clear();
disconnect();
navigate('/login');
}, [navigate]);
}, [disconnect, navigate]);
return (
<motion.nav