Implement authentication with SIWE #4
@ -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;
|
||||||
|
@ -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 = '/';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user