From a64323d1a4508a2bf440a7d8583ec553cae4eb51 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Sat, 14 Dec 2024 23:29:28 +1100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=91=20fix=20borked=20login=20redir?= =?UTF-8?q?ection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.ts | 14 +++++++++- frontend/src/Login/Login.tsx | 4 +-- .../SettingsNavbar/SettingsNavbar.tsx | 27 ++++++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index 4830b11..e70abc9 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -333,12 +333,24 @@ app.post('/auth/login', async (req: TypedRequest, res: Response) => { req.session.userId = user.id; req.session.cookie.expires = dayjs().add(1, 'week').toDate(); req.session.save(); // Explicitly save session to Redis - return res.status(200).json({ message: 'ok' }); + return res.status(200).json({ + id: user.id, + username: user.username, + email: user.email, + dateJoined: user.dateJoined, + profilePicture: user.profilePicture, + } as SanitisedUser); } catch (error) { return res.status(500).json({ error: 'Error logging in' }); } }); +app.post('/auth/logout', async (req: Request, res: Response) => { + req.session.destroy(() => { + return res.status(200).json({ message: 'ok' }); + }); +}); + app.post( '/discord/login', async (req: TypedRequest, res: Response) => { diff --git a/frontend/src/Login/Login.tsx b/frontend/src/Login/Login.tsx index e63042b..db005fa 100644 --- a/frontend/src/Login/Login.tsx +++ b/frontend/src/Login/Login.tsx @@ -13,7 +13,6 @@ export default function LoginPage() { const [password, setPassword] = useState(''); const [error, setError] = useState(undefined); const [success, setSuccess] = useState(undefined); - const navigate = useNavigate(); const { setUser } = useContext(UserContext); async function handleSubmit(event: FormEvent) { @@ -35,10 +34,9 @@ export default function LoginPage() { setError(errorHandler(json.error)); } else if (setUser) { setError(undefined); - setUser(json as User); setSuccess('Logged in successfully! Redirecting...'); setTimeout(() => { - navigate('/timeline'); + setUser(json as User); }, 1000); } else { setError(errorHandler("Couldn't update user object.")); diff --git a/frontend/src/Settings/SettingsNavbar/SettingsNavbar.tsx b/frontend/src/Settings/SettingsNavbar/SettingsNavbar.tsx index b9ddc15..7c5fbd8 100644 --- a/frontend/src/Settings/SettingsNavbar/SettingsNavbar.tsx +++ b/frontend/src/Settings/SettingsNavbar/SettingsNavbar.tsx @@ -7,12 +7,14 @@ import { } from '@heroicons/react/24/outline'; import classes from './SettingsNavbar.module.css'; import { NavLink, useLocation } from 'react-router'; +import { useContext } from 'react'; +import { User, UserContext } from '../../UserContext/UserContext'; interface Row { icon: React.ReactNode; name: string; to?: string; - onClick?: () => void; + onClick?: (() => void) | ((state: any) => void); } const rows: Row[][] = [ @@ -46,8 +48,18 @@ const rows: Row[][] = [ { icon: , name: 'Log out', - onClick: () => { - alert('hello'); + onClick: async (state: { + setUser: React.Dispatch>; + }) => { + const logout = await fetch('http://localhost:5180/auth/logout', { + method: 'POST', + credentials: 'include', + }); + if (logout.ok) { + state.setUser(null); + } else { + alert('Failed to logout.'); + } }, }, ], @@ -55,6 +67,7 @@ const rows: Row[][] = [ export function SettingsNavbar() { const { pathname } = useLocation(); + const { setUser } = useContext(UserContext); return (