owltide/server/api/auth/session.get.ts
Hornwitser 29b34deef0 Make session cookie permament
Set a max age for the session cookie to prevent it from expiring when
the browser is closed.  To prevent the age limit from being being
reached the session cookie is refreshed every time the session is
loaded.  This should fix login being lost when the browser is stopped.
2025-03-11 16:30:51 +01:00

21 lines
652 B
TypeScript

import { readAccounts, readSubscriptions } from "~/server/database";
import { AccountSession } from "~/shared/types/account";
export default defineEventHandler(async (event): Promise<AccountSession | undefined> => {
const session = await getAccountSession(event);
if (!session)
return;
const accounts = await readAccounts();
const subscriptions = await readSubscriptions();
const push = Boolean(
subscriptions.find(sub => sub.type === "push" && sub.sessionId === session.id)
);
await refreshAccountSession(event, session);
return {
id: session.id,
account: accounts.find(account => account.id === session.accountId)!,
push,
};
})