Move /api/account to /api/auth/account
All checks were successful
/ build (push) Successful in 1m16s
/ deploy (push) Successful in 16s

An account refers to the user the active session is logged in as. As
such it doesn't make sense outside of the /auth API paths that deals
with the current authenticated user.  Move /api/account to
/api/auth/account to reflect this.
This commit is contained in:
Hornwitser 2025-05-31 21:44:19 +02:00
parent e7dc00db54
commit 04b9707272
6 changed files with 5 additions and 5 deletions

View file

@ -0,0 +1,45 @@
import {
readAccounts, readSessions, readSubscriptions,
writeAccounts, writeSessions, writeSubscriptions,
} from "~/server/database";
import { cancelAccountStreams } from "~/server/streams";
export default defineEventHandler(async (event) => {
const accountSession = await requireAccountSession(event);
let accounts = await readAccounts();
const sessionAccount = accounts.find(
account => account.id === accountSession.accountId
);
if (!sessionAccount) {
throw Error("Account does not exist");
}
// Remove sessions for this account
const removedSessionIds = new Set<number>();
let sessions = await readSessions();
sessions = sessions.filter(session => {
if (session.accountId === accountSession.accountId) {
removedSessionIds.add(session.id);
return false;
}
return true;
});
cancelAccountStreams(accountSession.accountId);
await writeSessions(sessions);
await deleteCookie(event, "session");
// Remove subscriptions for this account
let subscriptions = await readSubscriptions();
subscriptions = subscriptions.filter(
subscription => !removedSessionIds.has(subscription.sessionId)
);
await writeSubscriptions(subscriptions);
// Remove the account
accounts = accounts.filter(account => account.id !== accountSession.accountId);
await writeAccounts(accounts);
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
})

View file

@ -0,0 +1,62 @@
import { Account } from "~/shared/types/account";
import { readAccounts, writeAccounts } from "~/server/database";
import { DateTime } from "luxon";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const body: Pick<Account, "interestedIds" | "timezone"> = await readBody(event);
if (
body.interestedIds !== undefined
&& (
!(body.interestedIds instanceof Array)
|| !body.interestedIds.every(id => typeof id === "string")
)
) {
throw createError({
status: 400,
message: "Invalid interestedIds",
});
}
if (body.timezone !== undefined) {
if (typeof body.timezone !== "string") {
throw createError({
status: 400,
message: "Invalid timezone",
});
}
if (body.timezone.length) {
const zonedTime = DateTime.local({ locale: "en-US" }).setZone(body.timezone);
if (!zonedTime.isValid) {
throw createError({
status: 400,
message: "Invalid timezone: " + zonedTime.invalidExplanation,
});
}
}
}
const accounts = await readAccounts();
const sessionAccount = accounts.find(account => account.id === session.accountId);
if (!sessionAccount) {
throw Error("Account does not exist");
}
if (body.interestedIds !== undefined) {
if (body.interestedIds.length) {
sessionAccount.interestedIds = body.interestedIds;
} else {
delete sessionAccount.interestedIds;
}
}
if (body.timezone !== undefined) {
if (body.timezone)
sessionAccount.timezone = body.timezone;
else
delete sessionAccount.timezone;
}
await writeAccounts(accounts);
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
})

View file

@ -0,0 +1,53 @@
import { readAccounts, writeAccounts, nextAccountId } from "~/server/database";
import { Account } from "~/shared/types/account";
export default defineEventHandler(async (event) => {
let session = await getAccountSession(event);
if (session) {
throw createError({
status: 409,
message: "Cannot create account while having an active session."
});
}
const formData = await readFormData(event);
const name = formData.get("name");
const accounts = await readAccounts();
let account: Account;
if (typeof name === "string") {
if (name === "") {
throw createError({
status: 400,
message: "Name cannot be blank",
});
}
if (accounts.some(account => account.name && account.name.toLowerCase() === name.toLowerCase())) {
throw createError({
status: 409,
message: "User already exists",
});
}
account = {
id: await nextAccountId(),
type: "regular",
name,
};
} else if (name === null) {
account = {
id: await nextAccountId(),
type: "anonymous",
};
} else {
throw createError({
status: 400,
message: "Invalid name",
});
}
accounts.push(account);
await writeAccounts(accounts);
await setAccountSession(event, account.id);
})