Skip to content

Commit

Permalink
Create logout route (#48)
Browse files Browse the repository at this point in the history
* Add logout route

* Add tests for logout route

* Fix failing tests
  • Loading branch information
Advayp authored Jan 20, 2025
1 parent 73b8569 commit 741f5fc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
16 changes: 16 additions & 0 deletions backend/app/api/user/logout/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GET } from './route';

jest.mock('@/lib/session', () => ({
verifySession: jest.fn(() => ({
isAuth: true,
uid: 3,
})),
deleteSession: jest.fn(() => {}),
}));

test('Logout succeeds for valid session', async () => {
const res = await GET();
const data = await res.json();

expect(data.success).toBeTruthy();
});
15 changes: 15 additions & 0 deletions backend/app/api/user/logout/noAuth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GET } from './route';

jest.mock('@/lib/session', () => ({
verifySession: jest.fn(() => ({
isAuth: false,
})),
deleteSession: jest.fn(() => {}),
}));

test('Logout fails for invalid session', async () => {
const res = await GET();
const data = await res.json();

expect(data.error).toEqual('User not authenticated');
});
20 changes: 20 additions & 0 deletions backend/app/api/user/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { deleteSession, verifySession } from '@/lib/session';
import { NextResponse } from 'next/server';

type GetResponse = {
success: boolean;
};

export const GET = async (): Promise<
NextResponse<GetResponse | { error: string }>
> => {
const currentSession = await verifySession();

if (!currentSession.isAuth) {
return NextResponse.json({ error: 'User not authenticated' });
}

await deleteSession();

return NextResponse.json({ success: true });
};

0 comments on commit 741f5fc

Please sign in to comment.