Skip to content

Remove stale cookies from login page #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ feedback and contributions are welcome!
- https://nextjs.org/docs/app/getting-started/layouts-and-pages (Creating new routes)
- We use `React-Hook-Form` for authentication: https://react-hook-form.com/
- We use useRouter to make API calls: https://nextjs.org/docs/app/api-reference/functions/use-router
- `NextResponse` for cookies: https://nextjs.org/docs/app/api-reference/functions/next-response

## Deploy on Vercel

Expand Down
6 changes: 6 additions & 0 deletions client/src/app/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import { login as apiLogin } from '@/lib/api/UserAPI';
import { setCookie } from '@/lib/services/CookieService';
import { CookieType } from '@/lib/types/enums';
import { getErrorMessage } from '@/lib/utils';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { getCookie } from 'cookies-next';

export async function login(email: string, password: string): Promise<string> {
let response;
try {
response = await apiLogin(email, password);
} catch (error) {
const cookieStore = await cookies();
cookieStore.delete(CookieType.ACCESS_TOKEN);
cookieStore.delete(CookieType.USER);
return getErrorMessage(error);
}
await setCookie(CookieType.ACCESS_TOKEN, response.token);
await setCookie(CookieType.USER, JSON.stringify(response.user));

redirect('/');
}
21 changes: 18 additions & 3 deletions client/src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import TextField from '@/components/TextField';
import Link from 'next/link';
import Alert from '@/components/Alert';
import { useForm, SubmitHandler } from 'react-hook-form';
import { UserAPI } from '@/lib/api';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { getErrorMessage } from '@/lib/utils';
import { useEffect, useState } from 'react';
import { login } from './login';
import { CookieType } from '@/lib/types/enums';
import { redirect } from 'next/navigation';
import { getCookie } from 'cookies-next';

interface LoginValues {
email: string;
Expand All @@ -34,9 +35,23 @@ export default function LoginPage() {
// If successful, the page will redirect and the rest of this function will
// not run
const error = await login(credentials.email, credentials.password);

console.log(`Error: ${error}`);
setError(error);
};

useEffect(() => {
const userCookie = getCookie(CookieType.USER);

// Send the user to the dashboard page if they already have a valid cookie
if (userCookie) {
console.log('Already logged in');
redirect('/');
}
}, []);

const userCookie = getCookie(CookieType.USER);

return (
<main className={styles.main}>
<div className={`${styles.login}`}>
Expand Down
1 change: 1 addition & 0 deletions client/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CookieType } from './lib/types/enums';
export function middleware(request: NextRequest) {
const userCookie = request.cookies.get(CookieType.USER);

// Send the user to the login page if they don't have a valid cookie
if (!userCookie) {
return NextResponse.redirect(new URL('/login', request.url));
}
Expand Down
1 change: 1 addition & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Setup

1. Copy `.env.example` to `.env` and fill in the values
1. If you are on ACM @ UCSD Dev Team, fields are available in our google drive for step 1 and 2
2. Add Firebase Admin credentials to a file named `firebaseServiceAccountKey.json`, at the same level as the `.env` file.
3. Start up Docker Desktop
4. Spin up Postgres:
Expand Down
Loading