diff --git a/backend/app/api/user/logout/auth.test.ts b/backend/app/api/user/logout/auth.test.ts new file mode 100644 index 0000000..b50c3d4 --- /dev/null +++ b/backend/app/api/user/logout/auth.test.ts @@ -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(); +}); diff --git a/backend/app/api/user/logout/noAuth.test.ts b/backend/app/api/user/logout/noAuth.test.ts new file mode 100644 index 0000000..2c85181 --- /dev/null +++ b/backend/app/api/user/logout/noAuth.test.ts @@ -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'); +}); diff --git a/backend/app/api/user/logout/route.ts b/backend/app/api/user/logout/route.ts new file mode 100644 index 0000000..35acf1c --- /dev/null +++ b/backend/app/api/user/logout/route.ts @@ -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 +> => { + const currentSession = await verifySession(); + + if (!currentSession.isAuth) { + return NextResponse.json({ error: 'User not authenticated' }); + } + + await deleteSession(); + + return NextResponse.json({ success: true }); +};