Rename AcountSession to ServerSession
Start the work of clearly distingushing client side types, server side types and types shared over the API by renaming "AccountSession" and "Session" names used on the server to "ServerSession".
This commit is contained in:
parent
16191a8dd2
commit
251e83f640
15 changed files with 39 additions and 36 deletions
|
@ -1,8 +1,7 @@
|
||||||
import { readAccounts } from "~/server/database"
|
import { readAccounts } from "~/server/database"
|
||||||
import { requireAccountSession } from "~/server/utils/session";
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireServerSession(event);
|
||||||
const accounts = await readAccounts();
|
const accounts = await readAccounts();
|
||||||
const account = accounts.find(a => a.id === session.accountId);
|
const account = accounts.find(a => a.id === session.accountId);
|
||||||
if (!account) {
|
if (!account) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { deleteDatbase, readAccounts } from "~/server/database";
|
import { deleteDatbase, readAccounts } from "~/server/database";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireServerSession(event);
|
||||||
let accounts = await readAccounts();
|
let accounts = await readAccounts();
|
||||||
const sessionAccount = accounts.find(
|
const sessionAccount = accounts.find(
|
||||||
account => account.id === session.accountId
|
account => account.id === session.accountId
|
||||||
|
|
|
@ -5,11 +5,11 @@ import {
|
||||||
import { cancelAccountStreams } from "~/server/streams";
|
import { cancelAccountStreams } from "~/server/streams";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const accountSession = await requireAccountSession(event);
|
const serverSession = await requireServerSession(event);
|
||||||
|
|
||||||
let accounts = await readAccounts();
|
let accounts = await readAccounts();
|
||||||
const sessionAccount = accounts.find(
|
const sessionAccount = accounts.find(
|
||||||
account => account.id === accountSession.accountId
|
account => account.id === serverSession.accountId
|
||||||
);
|
);
|
||||||
if (!sessionAccount) {
|
if (!sessionAccount) {
|
||||||
throw Error("Account does not exist");
|
throw Error("Account does not exist");
|
||||||
|
@ -19,13 +19,13 @@ export default defineEventHandler(async (event) => {
|
||||||
const removedSessionIds = new Set<number>();
|
const removedSessionIds = new Set<number>();
|
||||||
let sessions = await readSessions();
|
let sessions = await readSessions();
|
||||||
sessions = sessions.filter(session => {
|
sessions = sessions.filter(session => {
|
||||||
if (session.accountId === accountSession.accountId) {
|
if (session.accountId === serverSession.accountId) {
|
||||||
removedSessionIds.add(session.id);
|
removedSessionIds.add(session.id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
cancelAccountStreams(accountSession.accountId);
|
cancelAccountStreams(serverSession.accountId);
|
||||||
await writeSessions(sessions);
|
await writeSessions(sessions);
|
||||||
await deleteCookie(event, "session");
|
await deleteCookie(event, "session");
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ export default defineEventHandler(async (event) => {
|
||||||
await writeSubscriptions(subscriptions);
|
await writeSubscriptions(subscriptions);
|
||||||
|
|
||||||
// Remove the account
|
// Remove the account
|
||||||
accounts = accounts.filter(account => account.id !== accountSession.accountId);
|
accounts = accounts.filter(account => account.id !== serverSession.accountId);
|
||||||
await writeAccounts(accounts);
|
await writeAccounts(accounts);
|
||||||
|
|
||||||
// Update Schedule counts.
|
// Update Schedule counts.
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { readAccounts, writeAccounts } from "~/server/database";
|
||||||
import { DateTime } from "luxon";
|
import { DateTime } from "luxon";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireServerSession(event);
|
||||||
const body: Pick<Account, "interestedIds" | "timezone"> = await readBody(event);
|
const body: Pick<Account, "interestedIds" | "timezone"> = await readBody(event);
|
||||||
if (
|
if (
|
||||||
body.interestedIds !== undefined
|
body.interestedIds !== undefined
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { readAccounts, writeAccounts, nextAccountId } from "~/server/database";
|
||||||
import { Account } from "~/shared/types/account";
|
import { Account } from "~/shared/types/account";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
let session = await getAccountSession(event);
|
let session = await getServerSession(event);
|
||||||
if (session) {
|
if (session) {
|
||||||
throw createError({
|
throw createError({
|
||||||
status: 409,
|
status: 409,
|
||||||
|
@ -49,5 +49,5 @@ export default defineEventHandler(async (event) => {
|
||||||
|
|
||||||
accounts.push(account);
|
accounts.push(account);
|
||||||
await writeAccounts(accounts);
|
await writeAccounts(accounts);
|
||||||
await setAccountSession(event, account.id);
|
await setServerSession(event, account.id);
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,5 +14,5 @@ export default defineEventHandler(async (event) => {
|
||||||
return new Response(undefined, { status: 403 })
|
return new Response(undefined, { status: 403 })
|
||||||
}
|
}
|
||||||
|
|
||||||
await setAccountSession(event, account.id);
|
await setServerSession(event, account.id);
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { readAccounts } from "~/server/database";
|
||||||
import { cancelSessionStreams } from "~/server/streams";
|
import { cancelSessionStreams } from "~/server/streams";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await getAccountSession(event);
|
const session = await getServerSession(event);
|
||||||
if (session) {
|
if (session) {
|
||||||
const accounts = await readAccounts();
|
const accounts = await readAccounts();
|
||||||
const account = accounts.find(
|
const account = accounts.find(
|
||||||
|
@ -19,5 +19,5 @@ export default defineEventHandler(async (event) => {
|
||||||
if (session) {
|
if (session) {
|
||||||
cancelSessionStreams(session.id);
|
cancelSessionStreams(session.id);
|
||||||
}
|
}
|
||||||
await clearAccountSession(event);
|
await clearServerSession(event);
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { readAccounts, readSubscriptions } from "~/server/database";
|
||||||
import { AccountSession } from "~/shared/types/account";
|
import { AccountSession } from "~/shared/types/account";
|
||||||
|
|
||||||
export default defineEventHandler(async (event): Promise<AccountSession | undefined> => {
|
export default defineEventHandler(async (event): Promise<AccountSession | undefined> => {
|
||||||
const session = await getAccountSession(event);
|
const session = await getServerSession(event);
|
||||||
if (!session)
|
if (!session)
|
||||||
return;
|
return;
|
||||||
const accounts = await readAccounts();
|
const accounts = await readAccounts();
|
||||||
|
@ -11,7 +11,7 @@ export default defineEventHandler(async (event): Promise<AccountSession | undefi
|
||||||
subscriptions.find(sub => sub.type === "push" && sub.sessionId === session.id)
|
subscriptions.find(sub => sub.type === "push" && sub.sessionId === session.id)
|
||||||
);
|
);
|
||||||
|
|
||||||
await refreshAccountSession(event, session);
|
await refreshServerSession(event, session);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { addStream, deleteStream } from "~/server/streams";
|
||||||
import { readAccounts } from "~/server/database";
|
import { readAccounts } from "~/server/database";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await getAccountSession(event);
|
const session = await getServerSession(event);
|
||||||
let accountId: number | undefined;
|
let accountId: number | undefined;
|
||||||
if (session) {
|
if (session) {
|
||||||
const accounts = await readAccounts()
|
const accounts = await readAccounts()
|
||||||
|
|
|
@ -38,7 +38,7 @@ function isPatch(data: unknown): SchedulePatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireServerSession(event);
|
||||||
const accounts = await readAccounts();
|
const accounts = await readAccounts();
|
||||||
const account = accounts.find(a => a.id === session.accountId);
|
const account = accounts.find(a => a.id === session.accountId);
|
||||||
if (!account) {
|
if (!account) {
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Account } from "~/shared/types/account";
|
||||||
import { canSeeCrew } from "../utils/schedule";
|
import { canSeeCrew } from "../utils/schedule";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await getAccountSession(event);
|
const session = await getServerSession(event);
|
||||||
let account: Account | undefined;
|
let account: Account | undefined;
|
||||||
if (session) {
|
if (session) {
|
||||||
const accounts = await readAccounts()
|
const accounts = await readAccounts()
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
||||||
import { Subscription } from "~/shared/types/account";
|
import { Subscription } from "~/shared/types/account";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireServerSession(event);
|
||||||
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
||||||
const subscriptions = await readSubscriptions();
|
const subscriptions = await readSubscriptions();
|
||||||
const existingIndex = subscriptions.findIndex(
|
const existingIndex = subscriptions.findIndex(
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireServerSession(event);
|
||||||
const subscriptions = await readSubscriptions();
|
const subscriptions = await readSubscriptions();
|
||||||
const existingIndex = subscriptions.findIndex(
|
const existingIndex = subscriptions.findIndex(
|
||||||
sub => sub.type === "push" && sub.sessionId === session.id
|
sub => sub.type === "push" && sub.sessionId === session.id
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
import { readFile, unlink, writeFile } from "node:fs/promises";
|
import { readFile, unlink, writeFile } from "node:fs/promises";
|
||||||
import { Schedule } from "~/shared/types/schedule";
|
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";
|
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
|
// 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.
|
// this into proper application this should be replaced with an actual database.
|
||||||
|
|
||||||
|
@ -87,9 +92,9 @@ export async function nextSessionId() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readSessions() {
|
export async function readSessions() {
|
||||||
return await readJson<Session[]>(sessionsPath, []);
|
return await readJson<ServerSession[]>(sessionsPath, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeSessions(sessions: Session[]) {
|
export async function writeSessions(sessions: ServerSession[]) {
|
||||||
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
|
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import type { H3Event } from "h3";
|
import type { H3Event } from "h3";
|
||||||
import { nextSessionId, readSessions, readSubscriptions, writeSessions, writeSubscriptions } from "~/server/database";
|
import { nextSessionId, readSessions, readSubscriptions, ServerSession, writeSessions, writeSubscriptions } from "~/server/database";
|
||||||
import { Session } from "~/shared/types/account";
|
|
||||||
|
|
||||||
const oneYearSeconds = 365 * 24 * 60 * 60;
|
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");
|
const existingSessionCookie = await getSignedCookie(event, "session");
|
||||||
if (existingSessionCookie) {
|
if (existingSessionCookie) {
|
||||||
const sessionId = parseInt(existingSessionCookie, 10);
|
const sessionId = parseInt(existingSessionCookie, 10);
|
||||||
|
@ -27,19 +26,19 @@ async function clearAccountSessionInternal(event: H3Event, sessions: Session[])
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function clearAccountSession(event: H3Event) {
|
export async function clearServerSession(event: H3Event) {
|
||||||
const sessions = await readSessions();
|
const sessions = await readSessions();
|
||||||
if (await clearAccountSessionInternal(event, sessions)) {
|
if (await clearServerSessionInternal(event, sessions)) {
|
||||||
await writeSessions(sessions);
|
await writeSessions(sessions);
|
||||||
}
|
}
|
||||||
deleteCookie(event, "session");
|
deleteCookie(event, "session");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setAccountSession(event: H3Event, accountId: number) {
|
export async function setServerSession(event: H3Event, accountId: number) {
|
||||||
const sessions = await readSessions();
|
const sessions = await readSessions();
|
||||||
await clearAccountSessionInternal(event, sessions);
|
await clearServerSessionInternal(event, sessions);
|
||||||
|
|
||||||
const newSession: Session = {
|
const newSession: ServerSession = {
|
||||||
accountId,
|
accountId,
|
||||||
id: await nextSessionId(),
|
id: await nextSessionId(),
|
||||||
};
|
};
|
||||||
|
@ -49,11 +48,11 @@ export async function setAccountSession(event: H3Event, accountId: number) {
|
||||||
await setSignedCookie(event, "session", String(newSession.id), oneYearSeconds)
|
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)
|
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");
|
const sessionCookie = await getSignedCookie(event, "session");
|
||||||
if (sessionCookie) {
|
if (sessionCookie) {
|
||||||
const sessionId = parseInt(sessionCookie, 10);
|
const sessionId = parseInt(sessionCookie, 10);
|
||||||
|
@ -62,8 +61,8 @@ export async function getAccountSession(event: H3Event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requireAccountSession(event: H3Event) {
|
export async function requireServerSession(event: H3Event) {
|
||||||
const session = await getAccountSession(event);
|
const session = await getServerSession(event);
|
||||||
if (!session)
|
if (!session)
|
||||||
throw createError({
|
throw createError({
|
||||||
status: 401,
|
status: 401,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue