2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-07-09 15:21:39 +02:00
|
|
|
import { readUsers, writeUsers, nextUserId, type ServerUser, readAuthenticationMethods, nextAuthenticationMethodId, writeAuthenticationMethods } from "~/server/database";
|
2025-06-23 00:17:22 +02:00
|
|
|
import { broadcastEvent } from "~/server/streams";
|
2025-07-09 18:08:39 +02:00
|
|
|
import type { ApiSession } from "~/shared/types/api";
|
2025-03-07 23:53:57 +01:00
|
|
|
|
2025-07-09 15:21:39 +02:00
|
|
|
export default defineEventHandler(async (event): Promise<ApiSession> => {
|
2025-07-07 22:42:49 +02:00
|
|
|
let session = await getServerSession(event, false);
|
2025-07-09 15:21:39 +02:00
|
|
|
if (session?.accountId !== undefined) {
|
2025-03-07 23:53:57 +01:00
|
|
|
throw createError({
|
|
|
|
status: 409,
|
2025-07-09 15:21:39 +02:00
|
|
|
message: "Cannot create account while logged in to an account."
|
2025-03-07 23:53:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-07-09 17:57:49 +02:00
|
|
|
const body = await readBody(event);
|
|
|
|
const name = body?.name;
|
2025-03-07 23:53:57 +01:00
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
const users = await readUsers();
|
|
|
|
let user: ServerUser;
|
2025-03-07 23:53:57 +01:00
|
|
|
if (typeof name === "string") {
|
|
|
|
if (name === "") {
|
|
|
|
throw createError({
|
|
|
|
status: 400,
|
|
|
|
message: "Name cannot be blank",
|
|
|
|
});
|
|
|
|
}
|
2025-06-23 00:17:22 +02:00
|
|
|
if (users.some(user => user.name && user.name.toLowerCase() === name.toLowerCase())) {
|
2025-03-07 23:53:57 +01:00
|
|
|
throw createError({
|
|
|
|
status: 409,
|
|
|
|
message: "User already exists",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-06-28 01:25:43 +02:00
|
|
|
const firstUser = users.every(user => user.type === "anonymous");
|
2025-06-23 00:17:22 +02:00
|
|
|
user = {
|
|
|
|
id: await nextUserId(),
|
|
|
|
updatedAt: new Date().toISOString(),
|
2025-06-28 01:25:43 +02:00
|
|
|
type: firstUser ? "admin" : "regular",
|
2025-03-07 23:53:57 +01:00
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
2025-07-09 17:57:49 +02:00
|
|
|
} else if (name === undefined) {
|
2025-06-23 00:17:22 +02:00
|
|
|
user = {
|
|
|
|
id: await nextUserId(),
|
|
|
|
updatedAt: new Date().toISOString(),
|
2025-03-07 23:53:57 +01:00
|
|
|
type: "anonymous",
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw createError({
|
|
|
|
status: 400,
|
|
|
|
message: "Invalid name",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-07-09 17:57:49 +02:00
|
|
|
if (user.type !== "anonymous") {
|
|
|
|
if (!session?.authenticationProvider) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 409,
|
|
|
|
statusMessage: "Conflict",
|
|
|
|
message: "User account need an authentication method associated with it.",
|
|
|
|
});
|
|
|
|
}
|
2025-07-09 15:21:39 +02:00
|
|
|
const authMethods = await readAuthenticationMethods();
|
|
|
|
const method = authMethods.find(method => (
|
|
|
|
method.provider === session.authenticationProvider
|
|
|
|
&& method.slug === session.authenticationSlug
|
|
|
|
));
|
|
|
|
if (method) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 409,
|
|
|
|
statusMessage: "Conflict",
|
|
|
|
message: "A user is already associated with the authentication method",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
authMethods.push({
|
|
|
|
id: await nextAuthenticationMethodId(),
|
|
|
|
userId: user.id,
|
|
|
|
provider: session.authenticationProvider,
|
|
|
|
slug: session.authenticationSlug!,
|
|
|
|
name: session.authenticationName!,
|
|
|
|
})
|
|
|
|
await writeAuthenticationMethods(authMethods);
|
|
|
|
}
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
users.push(user);
|
|
|
|
await writeUsers(users);
|
|
|
|
await broadcastEvent({
|
|
|
|
type: "user-update",
|
|
|
|
data: user,
|
|
|
|
});
|
2025-07-09 15:21:39 +02:00
|
|
|
const newSession = await setServerSession(event, user);
|
|
|
|
return await serverSessionToApi(event, newSession);
|
2025-03-07 23:53:57 +01:00
|
|
|
})
|