-
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.
- Loading branch information
1 parent
2fec5ea
commit bcc110a
Showing
6 changed files
with
122 additions
and
31 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
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
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,23 @@ | ||
import { revokeSession } from '$lib/server/auth.js'; | ||
import { dataDirIsInit } from '$lib/server/data/dataDir.js'; | ||
import { error, json } from '@sveltejs/kit'; | ||
|
||
|
||
export async function POST({ request, cookies }) { | ||
const token = request.headers.get('Authorization'); | ||
if (!token) { | ||
return error(401, 'Authorization token is required'); | ||
} | ||
|
||
if (!await dataDirIsInit()) { | ||
return error(400, 'Server is not initialized'); | ||
} | ||
|
||
try { | ||
await revokeSession(token) | ||
} catch (e) { | ||
return error(401, `${e}`); | ||
} | ||
|
||
return json({}, { status: 200 }); | ||
} |
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
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
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,29 @@ | ||
/** Test cases for POST /api/admin/auth/logout */ | ||
|
||
import api from "$api"; | ||
import { expect, it } from "vitest"; | ||
import { setup } from "../../helpers"; | ||
|
||
it("Gives an error if the server isn't setup", async () => { | ||
const { token } = await setup(); | ||
await api.debug.clear(); | ||
await expect(api.admin.auth.logout(token)).rejects.toMatchObject({ code: 400 }); | ||
}); | ||
|
||
it('Gives an error for invalid tokens', async () => { | ||
const { token } = await setup(); | ||
await expect(api.admin.auth.logout(token + 'a')).rejects.toMatchObject({ code: 401 }); | ||
}); | ||
|
||
it('Gives an error for empty tokens', async () => { | ||
await setup(); | ||
await expect(api.admin.auth.logout('')).rejects.toMatchObject({ code: 401 }); | ||
}); | ||
|
||
it('Invalidates tokens', async () => { | ||
const { token } = await setup(); | ||
console.log(token); | ||
await expect(api.admin.auth.logout(token)).resolves.toStrictEqual({}); | ||
// Now that we're logged out, logging out again should fail | ||
await expect(api.admin.auth.logout(token)).rejects.toMatchObject({ code: 401 }); | ||
}); |