From 76e422ac92c079083e65eb6b65beebf28ddf43ae Mon Sep 17 00:00:00 2001 From: Neeraj Date: Thu, 17 Oct 2024 15:20:52 +0530 Subject: [PATCH] Implement logout handling when session is missing --- packages/backend/src/routes/auth.ts | 9 ++++++--- packages/frontend/src/App.tsx | 7 ++++++- .../frontend/src/components/shared/Sidebar/Sidebar.tsx | 5 ++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/routes/auth.ts b/packages/backend/src/routes/auth.ts index 5632e18c..8e2edbec 100644 --- a/packages/backend/src/routes/auth.ts +++ b/packages/backend/src/routes/auth.ts @@ -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; diff --git a/packages/frontend/src/App.tsx b/packages/frontend/src/App.tsx index 48390503..e1518029 100644 --- a/packages/frontend/src/App.tsx +++ b/packages/frontend/src/App.tsx @@ -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 = '/'; + } } }); }, []); diff --git a/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx b/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx index 6ed3b6fc..816c9bb1 100644 --- a/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx +++ b/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx @@ -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(); + 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 (