51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { createServiceClient } from "@/lib/supabase";
|
|
import { sendeRegistrierungsBestaetigung } from "@/lib/mailer";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
let body: { email?: string; password?: string; firma?: string };
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Ungültige Eingabe" }, { status: 422 });
|
|
}
|
|
|
|
const { email, password, firma } = body;
|
|
if (!email || !password || password.length < 8) {
|
|
return NextResponse.json({ error: "Ungültige Eingabe" }, { status: 422 });
|
|
}
|
|
|
|
const appUrl = process.env.APP_URL ?? "https://mbo-tech-it.de";
|
|
const db = createServiceClient();
|
|
|
|
const { data: linkData, error } = await db.auth.admin.generateLink({
|
|
type: "signup",
|
|
email,
|
|
password,
|
|
options: {
|
|
data: { firma: firma ?? "" },
|
|
redirectTo: `${appUrl}/auth/callback`,
|
|
},
|
|
});
|
|
|
|
if (error) {
|
|
if (
|
|
error.message.includes("already registered") ||
|
|
error.message.includes("already been registered")
|
|
) {
|
|
return NextResponse.json({ error: "already_registered" }, { status: 409 });
|
|
}
|
|
console.error("[Registrierung] Fehler:", error.message);
|
|
return NextResponse.json({ error: error.message }, { status: 500 });
|
|
}
|
|
|
|
const bestaetigungsLink = linkData.properties?.action_link;
|
|
if (!bestaetigungsLink) {
|
|
return NextResponse.json({ error: "Kein Bestätigungslink erhalten" }, { status: 500 });
|
|
}
|
|
|
|
await sendeRegistrierungsBestaetigung({ email, firma, bestaetigungsLink });
|
|
|
|
return NextResponse.json({ success: true });
|
|
}
|