Basic account and session system

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.
This commit is contained in:
Hornwitser 2025-03-07 12:41:57 +01:00
parent abdcc83eb9
commit 150cb82f5c
11 changed files with 276 additions and 4 deletions

View file

@ -1,13 +1,16 @@
import { readFile, writeFile } from "node:fs/promises";
import { Schedule } from "~/shared/types/schedule";
import { generateDemoSchedule } from "./generate-demo-schedule";
import { Subscription } from "~/shared/types/account";
import { Account, Subscription, Session } from "~/shared/types/account";
import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule";
// For this demo I'm just storing the runtime data in JSON files. When making
// this into proper application this should be replaced with an actual database.
const schedulePath = "data/schedule.json";
const subscriptionsPath = "data/subscriptions.json";
const accountsPath = "data/accounts.json";
const sessionsPath = "data/sessions.json";
const nextSessionIdPath = "data/next-session-id.json";
async function readJson<T>(filePath: string, fallback: T) {
let data: T extends () => infer R ? R : T;
@ -41,3 +44,25 @@ export async function readSubscriptions() {
export async function writeSubscriptions(subscriptions: Subscription[]) {
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
}
export async function readAccounts() {
return await readJson(accountsPath, generateDemoAccounts);
}
export async function writeAccounts(accounts: Account[]) {
await writeFile(accountsPath, JSON.stringify(accounts, undefined, "\t") + "\n", "utf-8");
}
export async function nextSessionId() {
const nextId = await readJson(nextSessionIdPath, 0);
await writeFile(nextSessionIdPath, String(nextId + 1), "utf-8");
return nextId;
}
export async function readSessions() {
return await readJson<Session[]>(sessionsPath, []);
}
export async function writeSessions(sessions: Session[]) {
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
}