Implement authentication with SIWE #4

Merged
nabarun merged 6 commits from nv-siwe into main 2024-10-18 12:47:12 +00:00
3 changed files with 16 additions and 5 deletions
Showing only changes of commit 76e422ac92 - Show all commits

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;
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