Session 6.3

This commit is contained in:
2026-04-10 21:56:31 +02:00
parent f20d5f09ae
commit 94bfd25553
24 changed files with 2398 additions and 104 deletions

View File

@@ -0,0 +1,70 @@
import { NextResponse } from "next/server";
import { registerCustomer } from "@/lib/zitadel";
import type { RegistrationInput } from "@/types";
import { z } from "zod";
const registrationSchema = z.object({
companyName: z.string().min(2).max(100),
givenName: z.string().min(1).max(100),
familyName: z.string().min(1).max(100),
email: z.string().email(),
preferredLanguage: z.enum(["en", "de", "fr", "it"]).optional(),
});
export async function POST(request: Request) {
try {
const body = await request.json();
const parsed = registrationSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: "Validation failed", details: parsed.error.flatten() },
{ status: 400 }
);
}
const input: RegistrationInput = parsed.data;
const result = await registerCustomer({
companyName: input.companyName,
email: input.email,
givenName: input.givenName,
familyName: input.familyName,
preferredLanguage: input.preferredLanguage,
});
return NextResponse.json(
{
orgId: result.orgId,
userId: result.userId,
message: "Registration successful. You will receive an invitation email to set your password.",
},
{ status: 201 }
);
} catch (e: any) {
console.error("Registration failed:", e);
const zitadelMessage = extractZitadelMessage(e.message);
return NextResponse.json(
{ error: zitadelMessage || "Registration failed. Please try again." },
{ status: e.statusCode || 500 }
);
}
}
/**
* ZITADEL errors come as:
* "ZITADEL POST /path: 400 {"code":3, "message":"..."}"
* Extract the human-readable "message" field.
*/
function extractZitadelMessage(errorMsg: string): string | null {
try {
const jsonStart = errorMsg.indexOf("{");
if (jsonStart === -1) return null;
const json = JSON.parse(errorMsg.slice(jsonStart));
return json.message || null;
} catch {
return null;
}
}