Skip to content
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

Remove stale cookies from login page #78

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions client/src/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { deleteUserCookies } from '@/lib/services/CookieService';
import { redirect } from 'next/navigation';

export async function GET(request: Request) {
await deleteUserCookies();
redirect('/login');
}
2 changes: 2 additions & 0 deletions client/src/app/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export async function login(email: string, password: string): Promise<string> {
try {
response = await apiLogin(email, password);
} catch (error) {
redirect('/api/logout');
return getErrorMessage(error);
}
await setCookie(CookieType.ACCESS_TOKEN, response.token);
await setCookie(CookieType.USER, JSON.stringify(response.user));

redirect('/');
}
20 changes: 15 additions & 5 deletions client/src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ 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 @@ -22,7 +22,6 @@ interface LoginValues {

export default function LoginPage() {
const [error, setError] = useState<string | undefined>(undefined);
const router = useRouter();

const {
register,
Expand All @@ -34,9 +33,20 @@ 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);

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('/');
}
}, []);

return (
<main className={styles.main}>
<div className={`${styles.login}`}>
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function Home() {

if (!accessToken) {
console.log('Dashboard broke: access token undefined');
redirect('/login');
redirect('/api/logout');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might also need to update the application and profile edit pages

}

try {
Expand All @@ -31,6 +31,6 @@ export default async function Home() {
);
} catch (error) {
console.log('Dashboard broke: error in fetching');
redirect('/login');
redirect('/api/logout');
}
}
12 changes: 12 additions & 0 deletions client/src/components/Logout/index.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can delete this, we switched to using a route handler instead

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useEffect } from 'react';
import { deleteUserCookies } from '@/lib/services/CookieService';

const Logout = () => {
// useEffect for server actions
useEffect(() => {
deleteUserCookies();
});
return <></>;
};

export default Logout;
1 change: 1 addition & 0 deletions client/src/lib/api/UserAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getErrorMessage } from '../utils';

export const login = async (email: string, password: string): Promise<LoginResponse> => {
const requestUrl = `${config.api.baseUrl}${config.api.endpoints.auth.login}`;
console.log('requestUrl', requestUrl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove 🙏

const requestBody: LoginRequest = { email, password };
const response = await axios.post<LoginResponse>(requestUrl, requestBody);
return response.data;
Expand Down
7 changes: 7 additions & 0 deletions client/src/lib/services/CookieService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cookies, headers } from 'next/headers';
import { CookieType } from '../types/enums';

export const getCookie = async (key: string): Promise<string> => {
const cookie = await cookies();
Expand All @@ -9,3 +10,9 @@ export const setCookie = async (key: string, value: string): Promise<void> => {
const cookie = await cookies();
cookie.set(key, value);
};

export const deleteUserCookies = async (): Promise<void> => {
const cookieStore = await cookies();
cookieStore.delete(CookieType.ACCESS_TOKEN);
cookieStore.delete(CookieType.USER);
};
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you undo this change (so there are no changes to the server/ folder), otherwise I think merging your PR might take down both portals while the backend action runs

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