2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-03-07 12:41:57 +01:00
|
|
|
import type { H3Event } from "h3";
|
2025-06-23 00:17:22 +02:00
|
|
|
import { nextSessionId, readSessions, readSubscriptions, type ServerSession, type ServerUser, writeSessions, writeSubscriptions } from "~/server/database";
|
2025-03-07 12:41:57 +01:00
|
|
|
|
2025-03-11 16:30:51 +01:00
|
|
|
const oneYearSeconds = 365 * 24 * 60 * 60;
|
|
|
|
|
2025-03-07 14:11:07 +01:00
|
|
|
async function removeSessionSubscription(sessionId: number) {
|
|
|
|
const subscriptions = await readSubscriptions();
|
|
|
|
const index = subscriptions.findIndex(subscription => subscription.sessionId === sessionId);
|
|
|
|
if (index !== -1) {
|
|
|
|
subscriptions.splice(index, 1);
|
|
|
|
await writeSubscriptions(subscriptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
async function clearServerSessionInternal(event: H3Event, sessions: ServerSession[]) {
|
2025-03-07 12:41:57 +01:00
|
|
|
const existingSessionCookie = await getSignedCookie(event, "session");
|
|
|
|
if (existingSessionCookie) {
|
|
|
|
const sessionId = parseInt(existingSessionCookie, 10);
|
|
|
|
const sessionIndex = sessions.findIndex(session => session.id === sessionId);
|
|
|
|
if (sessionIndex !== -1) {
|
|
|
|
sessions.splice(sessionIndex, 1);
|
2025-03-07 14:11:07 +01:00
|
|
|
await removeSessionSubscription(sessionId);
|
2025-03-07 12:41:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
export async function clearServerSession(event: H3Event) {
|
2025-03-07 12:41:57 +01:00
|
|
|
const sessions = await readSessions();
|
2025-06-09 16:51:05 +02:00
|
|
|
if (await clearServerSessionInternal(event, sessions)) {
|
2025-03-07 12:41:57 +01:00
|
|
|
await writeSessions(sessions);
|
|
|
|
}
|
2025-03-08 00:36:10 +01:00
|
|
|
deleteCookie(event, "session");
|
2025-03-07 12:41:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
export async function setServerSession(event: H3Event, account: ServerUser) {
|
2025-03-07 12:41:57 +01:00
|
|
|
const sessions = await readSessions();
|
2025-06-09 16:51:05 +02:00
|
|
|
await clearServerSessionInternal(event, sessions);
|
2025-03-07 12:41:57 +01:00
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
const newSession: ServerSession = {
|
2025-06-23 00:17:22 +02:00
|
|
|
account,
|
2025-03-07 12:41:57 +01:00
|
|
|
id: await nextSessionId(),
|
|
|
|
};
|
|
|
|
|
|
|
|
sessions.push(newSession);
|
|
|
|
await writeSessions(sessions);
|
2025-03-11 16:30:51 +01:00
|
|
|
await setSignedCookie(event, "session", String(newSession.id), oneYearSeconds)
|
|
|
|
}
|
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
export async function refreshServerSession(event: H3Event, session: ServerSession) {
|
2025-03-11 16:30:51 +01:00
|
|
|
await setSignedCookie(event, "session", String(session.id), oneYearSeconds)
|
2025-03-07 12:41:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
export async function getServerSession(event: H3Event) {
|
2025-03-07 12:41:57 +01:00
|
|
|
const sessionCookie = await getSignedCookie(event, "session");
|
|
|
|
if (sessionCookie) {
|
|
|
|
const sessionId = parseInt(sessionCookie, 10);
|
|
|
|
const sessions = await readSessions();
|
|
|
|
return sessions.find(session => session.id === sessionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
export async function requireServerSession(event: H3Event) {
|
|
|
|
const session = await getServerSession(event);
|
2025-03-07 12:41:57 +01:00
|
|
|
if (!session)
|
|
|
|
throw createError({
|
|
|
|
status: 401,
|
|
|
|
message: "Account session required",
|
|
|
|
});
|
|
|
|
return session;
|
|
|
|
}
|
2025-06-28 00:55:26 +02:00
|
|
|
|
|
|
|
export async function requireServerSessionWithAdmin(event: H3Event) {
|
|
|
|
const session = await requireServerSession(event);
|
|
|
|
if (session.account.type !== "admin") {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 403,
|
|
|
|
statusMessage: "Forbidden",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return session;
|
|
|
|
}
|