Move /api/account to /api/auth/account
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:
parent
e7dc00db54
commit
04b9707272
6 changed files with 5 additions and 5 deletions
53
server/api/auth/account.post.ts
Normal file
53
server/api/auth/account.post.ts
Normal 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);
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue