Provide a basic account system with login and server side session store identified by a cookie. Upon successful login a signed session cookie is set by the server with the session stored on the server identifying which account it is logged in as. The client uses a shared useFetch on the session endpoint to identify if it's logged in and which account it is logged in as, and refreshes this when loggin in or out.
14 lines
408 B
TypeScript
14 lines
408 B
TypeScript
import { readAccounts } from "~/server/database";
|
|
import { AccountSession } from "~/shared/types/account";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const session = await getAccountSession(event);
|
|
if (!session)
|
|
return;
|
|
const accounts = await readAccounts();
|
|
|
|
return {
|
|
id: session.id,
|
|
account: accounts.find(account => account.id === session.accountId)!,
|
|
} satisfies AccountSession;
|
|
})
|