add code signup to external api
deploy / build-and-deploy (push) Successful in 19s

This commit is contained in:
bytedream committed 2026-10-30 10:45:24 +01:00
1 parent f2ca4747a6
commit 0fc0024cb6
2 files changed
+32 -20

No files matched your search

+7 -4
View File
@@ -167,24 +167,27 @@
// Geburtstag im YYYY-MM-DD Format // Geburtstag im YYYY-MM-DD Format
"birthday": string, "birthday": string,
// Telefonnummer des Spielers // Telefonnummer des Spielers
"phone": string, "phone": string | null,
// Minecraft-Username // Minecraft-Username
"username": string, "username": string,
// Minecraft-UUID mit Bindestrichen // Minecraft-UUID mit Bindestrichen
"uuid": string, "uuid": string,
// Minecraft-Edition // Minecraft-Edition
"edition": "java" | "bedrock" "edition": "java" | "bedrock",
// Einladungscode einer direkten Einladung (optional)
"code": string | null | undefined
} }
``` ```
##### Response Codes ##### Response Codes
| http code | beschreibung | | http code | beschreibung |
| --------- | ------------------------------------------ | | --------- | ----------------------------------------------------------------------------------------------- |
| 201 | Der User wurde erstellt | | 201 | Der User wurde erstellt |
| 400 | Der Request Body ist falsch | | 400 | Der Request Body ist falsch |
| 401 | Es wurde ein falsches API Secret angegeben | | 401 | Es wurde ein falsches API Secret angegeben |
| 409 | Username oder UUID existiert bereits | | 404 | Der Einladungscode existiert nicht |
| 409 | Der Einladungscode wurde bereits verwendet oder der User (Username oder UUID) existiert bereits |
</details> </details>
+25 -16
View File
@@ -5,39 +5,48 @@ import { externalApi } from '../_api.ts';
export const POST = externalApi({ export const POST = externalApi({
auth: true, auth: true,
input: z.object({ input: z.object({
firstname: z.string().trim().min(1), firstname: z.string().trim(),
lastname: z.string().trim().min(1), lastname: z.string().trim(),
birthday: z.string().date(), birthday: z.string().date(),
phone: z.string().trim(), phone: z.string().trim().nullable(),
username: z.string().trim().min(1), username: z.string().trim(),
uuid: z.string().uuid(), uuid: z.string().uuid(),
edition: z.enum(['java', 'bedrock']) edition: z.enum(['java', 'bedrock']),
code: z.string().nullish()
}), }),
handler: async ({ input }) => { handler: async ({ input }) => {
if (input.code) {
const directSignup = await db.getDirectSignupByCode({ code: input.code });
// check if direct signup code exists
if (!directSignup) return new Response(null, { status: 404 });
// check if direct signup code was already used
else if (directSignup.user) return new Response(null, { status: 409 });
}
const existing = await db.existsUser({ const existing = await db.existsUser({
username: input.username, username: input.username,
uuid: input.uuid uuid: input.uuid
}); });
// check if user is already registered
if (existing) return new Response(null, { status: 409 });
if (existing) { await db.transaction(async (tx) => {
return new Response(JSON.stringify({ error: 'User with this username or uuid already exists' }), { const user = await tx.addUser({
status: 409,
headers: {
'Content-Type': 'application/json'
}
});
}
await db.addUser({
firstname: input.firstname, firstname: input.firstname,
lastname: input.lastname, lastname: input.lastname,
birthday: input.birthday, birthday: input.birthday,
telephone: input.phone || null, telephone: input.phone,
username: input.username, username: input.username,
edition: input.edition, edition: input.edition,
uuid: input.uuid uuid: input.uuid
}); });
if (input.code) {
await tx.setDirectSignupUser({ code: input.code, userId: user.id });
}
});
return new Response(null, { status: 201 }); return new Response(null, { status: 201 });
} }
}); });