139 lines
3.6 KiB
TypeScript
139 lines
3.6 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import { randomUUID } from "crypto";
|
|
import { formatPhoneDisplay } from "@/app/features/UserRoles/utils";
|
|
|
|
const BE_BASE_URL = process.env.BE_BASE_URL || "http://localhost:8583";
|
|
const COOKIE_NAME = "auth_token";
|
|
|
|
// Interface matching the backend RegisterRequest
|
|
interface RegisterRequest {
|
|
creator: string;
|
|
email: string;
|
|
first_name: string;
|
|
groups: string[];
|
|
job_title: string;
|
|
last_name: string;
|
|
merchants: string[];
|
|
phone: string;
|
|
username: string;
|
|
}
|
|
|
|
// Frontend form interface
|
|
interface FrontendRegisterForm {
|
|
email: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
username: string;
|
|
phone?: string;
|
|
jobTitle?: string;
|
|
groups?: string[];
|
|
merchants?: string[];
|
|
creator?: string;
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body: FrontendRegisterForm = await request.json();
|
|
|
|
// Get the auth token from cookies
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get(COOKIE_NAME)?.value;
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: "No authentication token found",
|
|
},
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Validate required fields
|
|
const requiredFields = ["email", "firstName", "lastName", "username"];
|
|
const missingFields = requiredFields.filter(
|
|
field => !body[field as keyof FrontendRegisterForm]
|
|
);
|
|
|
|
if (missingFields.length > 0) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: `Missing required fields: ${missingFields.join(", ")}`,
|
|
},
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Map frontend payload to backend RegisterRequest format
|
|
const registerPayload: RegisterRequest = {
|
|
creator: body.creator || randomUUID(), // Generate UUID if not provided
|
|
email: body.email,
|
|
first_name: body.firstName,
|
|
groups: body.groups || ["Reader"], // Default to empty array if not provided
|
|
job_title: body.jobTitle || "Reader",
|
|
last_name: body.lastName,
|
|
merchants: body.merchants || ["Win Bot"], // Default to empty array if not provided
|
|
phone: body.phone ? formatPhoneDisplay(body.phone, body.countryCode) : "",
|
|
username: body.username,
|
|
};
|
|
|
|
// Call backend registration endpoint
|
|
const resp = await fetch(`${BE_BASE_URL}/api/v1/auth/register`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(registerPayload),
|
|
});
|
|
|
|
console.log("[DEBUG] [REGISTER-PAYLOAD]: ", registerPayload);
|
|
|
|
// Handle backend response
|
|
if (!resp.ok) {
|
|
const errorData = await safeJson(resp);
|
|
console.log("[DEBUG] [REGISTER-ERROR]: ", errorData);
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: errorData?.message || "Registration failed",
|
|
},
|
|
{ status: resp.status }
|
|
);
|
|
}
|
|
|
|
const data = await resp.json();
|
|
|
|
console.log("[DEBUG] [REGISTER]: ", data);
|
|
|
|
return NextResponse.json(
|
|
{
|
|
success: true,
|
|
message: "Registration successful",
|
|
user: data.user || null,
|
|
},
|
|
{ status: 201 }
|
|
);
|
|
} catch (error) {
|
|
console.error("Registration proxy error:", error);
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: "Internal server error during registration",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// Helper function to safely parse JSON responses
|
|
async function safeJson(resp: Response) {
|
|
try {
|
|
return await resp.json();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|