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
+40 -28

No files matched your search

+29 -20
View File
@@ -5,37 +5,46 @@ import { externalApi } from '../_api.ts';
export const POST = externalApi({
auth: true,
input: z.object({
firstname: z.string().trim().min(1),
lastname: z.string().trim().min(1),
firstname: z.string().trim(),
lastname: z.string().trim(),
birthday: z.string().date(),
phone: z.string().trim(),
username: z.string().trim().min(1),
phone: z.string().trim().nullable(),
username: z.string().trim(),
uuid: z.string().uuid(),
edition: z.enum(['java', 'bedrock'])
edition: z.enum(['java', 'bedrock']),
code: z.string().nullish()
}),
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({
username: input.username,
uuid: input.uuid
});
// check if user is already registered
if (existing) return new Response(null, { status: 409 });
if (existing) {
return new Response(JSON.stringify({ error: 'User with this username or uuid already exists' }), {
status: 409,
headers: {
'Content-Type': 'application/json'
}
await db.transaction(async (tx) => {
const user = await tx.addUser({
firstname: input.firstname,
lastname: input.lastname,
birthday: input.birthday,
telephone: input.phone,
username: input.username,
edition: input.edition,
uuid: input.uuid
});
}
await db.addUser({
firstname: input.firstname,
lastname: input.lastname,
birthday: input.birthday,
telephone: input.phone || null,
username: input.username,
edition: input.edition,
uuid: input.uuid
if (input.code) {
await tx.setDirectSignupUser({ code: input.code, userId: user.id });
}
});
return new Response(null, { status: 201 });