2023-11-06 02:57:00 +01:00
|
|
|
import { defineEventHandler, getCookie, deleteCookie } from "h3";
|
2023-05-11 06:03:22 +02:00
|
|
|
|
|
|
|
import { isAuthorised } from "../middleware/auth";
|
|
|
|
import { database } from "../utils/database";
|
|
|
|
import { cookieSettings } from "../utils/rootUtils";
|
|
|
|
|
2023-11-06 02:57:00 +01:00
|
|
|
import { createError } from "#imports";
|
|
|
|
|
2023-05-11 06:03:22 +02:00
|
|
|
export default defineEventHandler(async (e) => {
|
|
|
|
const token = getCookie(e, "token");
|
|
|
|
if (token === undefined) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 401,
|
|
|
|
data: "You can't log out if you're already logged out (no session cookie)",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteCookie(e, "token", cookieSettings);
|
|
|
|
if (!await isAuthorised(token)) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 401,
|
|
|
|
message: "You can't log out if you're already logged out (session expired or never existed)",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
database.session.delete({
|
|
|
|
where: {
|
|
|
|
id: BigInt(token),
|
|
|
|
},
|
|
|
|
});
|
2023-05-11 06:03:22 +02:00
|
|
|
return { message: "Logged out" };
|
|
|
|
});
|