diff --git a/server/api/accounts/index.get.ts b/server/api/accounts/index.get.ts index 4713a62..4fd9c24 100644 --- a/server/api/accounts/index.get.ts +++ b/server/api/accounts/index.get.ts @@ -1,8 +1,7 @@ import { readAccounts } from "~/server/database" -import { requireAccountSession } from "~/server/utils/session"; export default defineEventHandler(async (event) => { - const session = await requireAccountSession(event); + const session = await requireServerSession(event); const accounts = await readAccounts(); const account = accounts.find(a => a.id === session.accountId); if (!account) { diff --git a/server/api/admin/delete-database.post.ts b/server/api/admin/delete-database.post.ts index 1809ced..9537acf 100644 --- a/server/api/admin/delete-database.post.ts +++ b/server/api/admin/delete-database.post.ts @@ -1,7 +1,7 @@ import { deleteDatbase, readAccounts } from "~/server/database"; export default defineEventHandler(async (event) => { - const session = await requireAccountSession(event); + const session = await requireServerSession(event); let accounts = await readAccounts(); const sessionAccount = accounts.find( account => account.id === session.accountId diff --git a/server/api/auth/account.delete.ts b/server/api/auth/account.delete.ts index 1968681..5d8e338 100644 --- a/server/api/auth/account.delete.ts +++ b/server/api/auth/account.delete.ts @@ -5,11 +5,11 @@ import { import { cancelAccountStreams } from "~/server/streams"; export default defineEventHandler(async (event) => { - const accountSession = await requireAccountSession(event); + const serverSession = await requireServerSession(event); let accounts = await readAccounts(); const sessionAccount = accounts.find( - account => account.id === accountSession.accountId + account => account.id === serverSession.accountId ); if (!sessionAccount) { throw Error("Account does not exist"); @@ -19,13 +19,13 @@ export default defineEventHandler(async (event) => { const removedSessionIds = new Set(); let sessions = await readSessions(); sessions = sessions.filter(session => { - if (session.accountId === accountSession.accountId) { + if (session.accountId === serverSession.accountId) { removedSessionIds.add(session.id); return false; } return true; }); - cancelAccountStreams(accountSession.accountId); + cancelAccountStreams(serverSession.accountId); await writeSessions(sessions); await deleteCookie(event, "session"); @@ -37,7 +37,7 @@ export default defineEventHandler(async (event) => { await writeSubscriptions(subscriptions); // Remove the account - accounts = accounts.filter(account => account.id !== accountSession.accountId); + accounts = accounts.filter(account => account.id !== serverSession.accountId); await writeAccounts(accounts); // Update Schedule counts. diff --git a/server/api/auth/account.patch.ts b/server/api/auth/account.patch.ts index d3149ca..0b3ded2 100644 --- a/server/api/auth/account.patch.ts +++ b/server/api/auth/account.patch.ts @@ -3,7 +3,7 @@ import { readAccounts, writeAccounts } from "~/server/database"; import { DateTime } from "luxon"; export default defineEventHandler(async (event) => { - const session = await requireAccountSession(event); + const session = await requireServerSession(event); const body: Pick = await readBody(event); if ( body.interestedIds !== undefined diff --git a/server/api/auth/account.post.ts b/server/api/auth/account.post.ts index 11b65cc..6603a4e 100644 --- a/server/api/auth/account.post.ts +++ b/server/api/auth/account.post.ts @@ -2,7 +2,7 @@ import { readAccounts, writeAccounts, nextAccountId } from "~/server/database"; import { Account } from "~/shared/types/account"; export default defineEventHandler(async (event) => { - let session = await getAccountSession(event); + let session = await getServerSession(event); if (session) { throw createError({ status: 409, @@ -49,5 +49,5 @@ export default defineEventHandler(async (event) => { accounts.push(account); await writeAccounts(accounts); - await setAccountSession(event, account.id); + await setServerSession(event, account.id); }) diff --git a/server/api/auth/login.post.ts b/server/api/auth/login.post.ts index 6a0a771..dfb62cd 100644 --- a/server/api/auth/login.post.ts +++ b/server/api/auth/login.post.ts @@ -14,5 +14,5 @@ export default defineEventHandler(async (event) => { return new Response(undefined, { status: 403 }) } - await setAccountSession(event, account.id); + await setServerSession(event, account.id); }) diff --git a/server/api/auth/session.delete.ts b/server/api/auth/session.delete.ts index 93a6db2..9f24fb4 100644 --- a/server/api/auth/session.delete.ts +++ b/server/api/auth/session.delete.ts @@ -2,7 +2,7 @@ import { readAccounts } from "~/server/database"; import { cancelSessionStreams } from "~/server/streams"; export default defineEventHandler(async (event) => { - const session = await getAccountSession(event); + const session = await getServerSession(event); if (session) { const accounts = await readAccounts(); const account = accounts.find( @@ -19,5 +19,5 @@ export default defineEventHandler(async (event) => { if (session) { cancelSessionStreams(session.id); } - await clearAccountSession(event); + await clearServerSession(event); }) diff --git a/server/api/auth/session.get.ts b/server/api/auth/session.get.ts index dbafcd3..03c1d43 100644 --- a/server/api/auth/session.get.ts +++ b/server/api/auth/session.get.ts @@ -2,7 +2,7 @@ import { readAccounts, readSubscriptions } from "~/server/database"; import { AccountSession } from "~/shared/types/account"; export default defineEventHandler(async (event): Promise => { - const session = await getAccountSession(event); + const session = await getServerSession(event); if (!session) return; const accounts = await readAccounts(); @@ -11,7 +11,7 @@ export default defineEventHandler(async (event): Promise sub.type === "push" && sub.sessionId === session.id) ); - await refreshAccountSession(event, session); + await refreshServerSession(event, session); return { id: session.id, diff --git a/server/api/events.ts b/server/api/events.ts index 54184b1..6df8f1d 100644 --- a/server/api/events.ts +++ b/server/api/events.ts @@ -3,7 +3,7 @@ import { addStream, deleteStream } from "~/server/streams"; import { readAccounts } from "~/server/database"; export default defineEventHandler(async (event) => { - const session = await getAccountSession(event); + const session = await getServerSession(event); let accountId: number | undefined; if (session) { const accounts = await readAccounts() diff --git a/server/api/schedule.patch.ts b/server/api/schedule.patch.ts index 473a7e7..4401168 100644 --- a/server/api/schedule.patch.ts +++ b/server/api/schedule.patch.ts @@ -38,7 +38,7 @@ function isPatch(data: unknown): SchedulePatch { } export default defineEventHandler(async (event) => { - const session = await requireAccountSession(event); + const session = await requireServerSession(event); const accounts = await readAccounts(); const account = accounts.find(a => a.id === session.accountId); if (!account) { diff --git a/server/api/schedule.ts b/server/api/schedule.ts index 5c73166..33dc8dc 100644 --- a/server/api/schedule.ts +++ b/server/api/schedule.ts @@ -3,7 +3,7 @@ import { Account } from "~/shared/types/account"; import { canSeeCrew } from "../utils/schedule"; export default defineEventHandler(async (event) => { - const session = await getAccountSession(event); + const session = await getServerSession(event); let account: Account | undefined; if (session) { const accounts = await readAccounts() diff --git a/server/api/subscribe.post.ts b/server/api/subscribe.post.ts index eb89685..4d04fe0 100644 --- a/server/api/subscribe.post.ts +++ b/server/api/subscribe.post.ts @@ -2,7 +2,7 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database"; import { Subscription } from "~/shared/types/account"; export default defineEventHandler(async (event) => { - const session = await requireAccountSession(event); + const session = await requireServerSession(event); const body: { subscription: PushSubscriptionJSON } = await readBody(event); const subscriptions = await readSubscriptions(); const existingIndex = subscriptions.findIndex( diff --git a/server/api/unsubscribe.post.ts b/server/api/unsubscribe.post.ts index 6f2874b..d6558ec 100644 --- a/server/api/unsubscribe.post.ts +++ b/server/api/unsubscribe.post.ts @@ -1,7 +1,7 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database"; export default defineEventHandler(async (event) => { - const session = await requireAccountSession(event); + const session = await requireServerSession(event); const subscriptions = await readSubscriptions(); const existingIndex = subscriptions.findIndex( sub => sub.type === "push" && sub.sessionId === session.id diff --git a/server/database.ts b/server/database.ts index 598ac40..aebc4d1 100644 --- a/server/database.ts +++ b/server/database.ts @@ -1,8 +1,13 @@ import { readFile, unlink, writeFile } from "node:fs/promises"; import { Schedule } from "~/shared/types/schedule"; -import { Account, Subscription, Session } from "~/shared/types/account"; +import { Account, Subscription } from "~/shared/types/account"; import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule"; +export interface ServerSession { + id: number, + accountId: number, +}; + // 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. @@ -87,9 +92,9 @@ export async function nextSessionId() { } export async function readSessions() { - return await readJson(sessionsPath, []); + return await readJson(sessionsPath, []); } -export async function writeSessions(sessions: Session[]) { +export async function writeSessions(sessions: ServerSession[]) { await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8"); } diff --git a/server/utils/session.ts b/server/utils/session.ts index ef2781f..51c0423 100644 --- a/server/utils/session.ts +++ b/server/utils/session.ts @@ -1,6 +1,5 @@ import type { H3Event } from "h3"; -import { nextSessionId, readSessions, readSubscriptions, writeSessions, writeSubscriptions } from "~/server/database"; -import { Session } from "~/shared/types/account"; +import { nextSessionId, readSessions, readSubscriptions, ServerSession, writeSessions, writeSubscriptions } from "~/server/database"; const oneYearSeconds = 365 * 24 * 60 * 60; @@ -13,7 +12,7 @@ async function removeSessionSubscription(sessionId: number) { } } -async function clearAccountSessionInternal(event: H3Event, sessions: Session[]) { +async function clearServerSessionInternal(event: H3Event, sessions: ServerSession[]) { const existingSessionCookie = await getSignedCookie(event, "session"); if (existingSessionCookie) { const sessionId = parseInt(existingSessionCookie, 10); @@ -27,19 +26,19 @@ async function clearAccountSessionInternal(event: H3Event, sessions: Session[]) return false; } -export async function clearAccountSession(event: H3Event) { +export async function clearServerSession(event: H3Event) { const sessions = await readSessions(); - if (await clearAccountSessionInternal(event, sessions)) { + if (await clearServerSessionInternal(event, sessions)) { await writeSessions(sessions); } deleteCookie(event, "session"); } -export async function setAccountSession(event: H3Event, accountId: number) { +export async function setServerSession(event: H3Event, accountId: number) { const sessions = await readSessions(); - await clearAccountSessionInternal(event, sessions); + await clearServerSessionInternal(event, sessions); - const newSession: Session = { + const newSession: ServerSession = { accountId, id: await nextSessionId(), }; @@ -49,11 +48,11 @@ export async function setAccountSession(event: H3Event, accountId: number) { await setSignedCookie(event, "session", String(newSession.id), oneYearSeconds) } -export async function refreshAccountSession(event: H3Event, session: Session) { +export async function refreshServerSession(event: H3Event, session: ServerSession) { await setSignedCookie(event, "session", String(session.id), oneYearSeconds) } -export async function getAccountSession(event: H3Event) { +export async function getServerSession(event: H3Event) { const sessionCookie = await getSignedCookie(event, "session"); if (sessionCookie) { const sessionId = parseInt(sessionCookie, 10); @@ -62,8 +61,8 @@ export async function getAccountSession(event: H3Event) { } } -export async function requireAccountSession(event: H3Event) { - const session = await getAccountSession(event); +export async function requireServerSession(event: H3Event) { + const session = await getServerSession(event); if (!session) throw createError({ status: 401,