Skip to content

Commit

Permalink
➡️ merge pull request #51 from devsoc-unsw/feature/createEventComponent
Browse files Browse the repository at this point in the history
🔑 fix borked login redirection
  • Loading branch information
lachlanshoesmith authored Dec 14, 2024
2 parents 2c16b00 + 4e2c0a6 commit 0cad749
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
14 changes: 13 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,24 @@ app.post('/auth/login', async (req: TypedRequest<LoginBody>, 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<DiscordLoginBody>, res: Response) => {
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TextInput, TextOptions } from '../TextInput/TextInput';
import { UserCircleIcon } from '@heroicons/react/24/outline';
import { LockClosedIcon } from '@heroicons/react/24/outline';
import { useState, FormEvent, useContext } from 'react';
import { Link, useNavigate } from 'react-router';
import { Link } from 'react-router';
import { errorHandler, AuthError } from '../errorHandler';
import { UserContext, User } from '../UserContext/UserContext';

Expand All @@ -13,7 +13,6 @@ export default function LoginPage() {
const [password, setPassword] = useState('');
const [error, setError] = useState<AuthError | undefined>(undefined);
const [success, setSuccess] = useState<string | undefined>(undefined);
const navigate = useNavigate();
const { setUser } = useContext(UserContext);

async function handleSubmit(event: FormEvent<HTMLFormElement>) {
Expand All @@ -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."));
Expand Down
27 changes: 23 additions & 4 deletions frontend/src/Settings/SettingsNavbar/SettingsNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[][] = [
Expand Down Expand Up @@ -46,15 +48,26 @@ const rows: Row[][] = [
{
icon: <KeyIcon />,
name: 'Log out',
onClick: () => {
alert('hello');
onClick: async (state: {
setUser: React.Dispatch<React.SetStateAction<User | null>>;
}) => {
const logout = await fetch('http://localhost:5180/auth/logout', {
method: 'POST',
credentials: 'include',
});
if (logout.ok) {
state.setUser(null);
} else {
alert('Failed to logout.');
}
},
},
],
];

export function SettingsNavbar() {
const { pathname } = useLocation();
const { setUser } = useContext(UserContext);

return (
<nav className={classes.container}>
Expand All @@ -68,7 +81,13 @@ export function SettingsNavbar() {
? `${classes.row} ${classes.interactable}`
: classes.row
}
{...(row.onClick && { onClick: row.onClick })}
{...{
onClick: (_) =>
row.onClick &&
row.onClick({
setUser,
}),
}}
>
<div className={classes.icon}>{row.icon}</div>
{row.to ? (
Expand Down

0 comments on commit 0cad749

Please sign in to comment.