-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add logout route * Add tests for logout route * Fix failing tests
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |