Files
nuxtfiber/server/api/auth/register.post.ts
Beyhan Oğur 7b2b27a42c first commit
2026-04-26 22:18:17 +03:00

38 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Proxy to backend POST /api/v1/auth/register (with Turnstile verification)
export default defineEventHandler(async (event) => {
const body = await readBody(event) as { turnstile_token?: string; email?: string; first_name?: string; last_name?: string; username?: string; password?: string };
const token = body.turnstile_token;
if (!token) {
throw createError({ statusCode: 422, statusMessage: 'Güvenlik doğrulaması gerekli.', message: 'Güvenlik doğrulaması gerekli.' });
}
const validation = await verifyTurnstileToken(token, event);
if (!validation.success) {
throw createError({ statusCode: 400, statusMessage: 'Güvenlik doğrulaması başarısız.', message: 'Güvenlik doğrulaması başarısız.' });
}
const config = useRuntimeConfig();
const publicConfig = config.public as Record<string, unknown>;
const API_BASE = config.public.BASE_API_URL || (publicConfig.NUXT_PUBLIC_API_BASE as string) || 'http://127.0.0.1:8080';
const res = await $fetch(`${API_BASE}/api/v1/auth/register`, {
method: 'POST',
body: {
email: body.email,
first_name: body.first_name,
last_name: body.last_name,
password: body.password,
username: body.username,
},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
}).catch((e: unknown) => {
const err = e as { data?: { statusCode?: number }; statusCode?: number; data?: { message?: string }; message?: string; response?: { _data?: unknown } };
throw createError({
statusCode: err?.data?.statusCode ?? err?.statusCode ?? 400,
statusMessage: (err?.data && typeof err.data === 'object' && 'message' in err.data ? (err.data as { message?: string }).message : null) ?? err?.message ?? 'Registration failed',
data: err?.data ?? err?.response?._data,
});
});
return res;
});