56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
import type { TokenResponse } from '@/types/auth';
|
|
import { HttpError, callDeployControl, callInstanceApi, normalizeTokenResponse } from '@/lib/runtime-control';
|
|
|
|
function errorStatus(error: unknown): number {
|
|
if (error instanceof HttpError) {
|
|
return error.status;
|
|
}
|
|
return 500;
|
|
}
|
|
|
|
function errorDetail(error: unknown): string {
|
|
if (error instanceof HttpError) {
|
|
return error.message;
|
|
}
|
|
return error instanceof Error ? error.message : 'registration failed';
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = (await request.json()) as {
|
|
username?: string;
|
|
email?: string;
|
|
password?: string;
|
|
};
|
|
const username = body.username?.trim() || '';
|
|
const email = body.email?.trim() || '';
|
|
const password = body.password || '';
|
|
|
|
if (!username || !password) {
|
|
return NextResponse.json({ detail: 'username and password are required' }, { status: 400 });
|
|
}
|
|
|
|
const routing = await callDeployControl<{
|
|
api_base_url?: string;
|
|
frontend_base_url?: string;
|
|
public_url?: string;
|
|
}>('/api/instances/register', {
|
|
username,
|
|
email,
|
|
password,
|
|
});
|
|
|
|
const response = await callInstanceApi<TokenResponse>(routing.api_base_url || '', '/api/auth/register', {
|
|
username,
|
|
email,
|
|
password,
|
|
});
|
|
|
|
return NextResponse.json(normalizeTokenResponse(response, routing));
|
|
} catch (error) {
|
|
return NextResponse.json({ detail: errorDetail(error) }, { status: errorStatus(error) });
|
|
}
|
|
}
|