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) => { router.post('/logout', (req, res) => {
// This is how you clear cookie-session req.session.destroy((err) => {
(req as any).session = null; if (err) {
res.send({ success: true }); return res.send({ success: false });
}
res.send({ success: true });
});
}); });
export default router; export default router;

View File

@ -59,12 +59,17 @@ function App() {
fetch(`${baseUrl}/auth/session`, { fetch(`${baseUrl}/auth/session`, {
credentials: 'include', credentials: 'include',
}).then((res) => { }).then((res) => {
const path = window.location.pathname;
if (res.status !== 200) { if (res.status !== 200) {
localStorage.clear(); localStorage.clear();
const path = window.location.pathname;
if (path !== '/login') { if (path !== '/login') {
window.location.pathname = '/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 { NavLink, useNavigate, useParams } from 'react-router-dom';
import { Organization, User } from 'gql-client'; import { Organization, User } from 'gql-client';
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
import { useDisconnect } from 'wagmi';
import { useGQLClient } from 'context/GQLClientContext'; import { useGQLClient } from 'context/GQLClientContext';
import { import {
@ -33,6 +34,7 @@ export const Sidebar = ({ mobileOpen }: SidebarProps) => {
const isDesktop = useMediaQuery('(min-width: 960px)'); const isDesktop = useMediaQuery('(min-width: 960px)');
const [user, setUser] = useState<User>(); const [user, setUser] = useState<User>();
const { disconnect } = useDisconnect();
const fetchUser = useCallback(async () => { const fetchUser = useCallback(async () => {
const { user } = await client.getUser(); const { user } = await client.getUser();
@ -89,8 +91,9 @@ export const Sidebar = ({ mobileOpen }: SidebarProps) => {
credentials: 'include', credentials: 'include',
}); });
localStorage.clear(); localStorage.clear();
disconnect();
navigate('/login'); navigate('/login');
}, [navigate]); }, [disconnect, navigate]);
return ( return (
<motion.nav <motion.nav