35 lines
906 B
TypeScript
35 lines
906 B
TypeScript
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
import { z } from 'zod';
|
||
|
|
|
||
|
|
import { errorResponse } from '@/lib/errors';
|
||
|
|
import { activateAccount } from '@/lib/services/portal-auth.service';
|
||
|
|
|
||
|
|
const bodySchema = z.object({
|
||
|
|
token: z.string().min(1),
|
||
|
|
password: z.string().min(12),
|
||
|
|
});
|
||
|
|
|
||
|
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||
|
|
let body: unknown;
|
||
|
|
try {
|
||
|
|
body = await req.json();
|
||
|
|
} catch {
|
||
|
|
return NextResponse.json({ error: 'Invalid request body' }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const parsed = bodySchema.safeParse(body);
|
||
|
|
if (!parsed.success) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: parsed.error.errors[0]?.message ?? 'Invalid input' },
|
||
|
|
{ status: 400 },
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await activateAccount(parsed.data.token, parsed.data.password);
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
} catch (err) {
|
||
|
|
return errorResponse(err);
|
||
|
|
}
|
||
|
|
}
|