import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/app/lib/auth"; import { signJWT } from "@/app/lib/jwt"; export async function POST(request: NextRequest) { try { const body = await request.json(); const { email, password, name } = body; // Validasyon if (!email || !password || !name) { return NextResponse.json( { error: "Email, password ve name gereklidir" }, { status: 400 } ); } if (password.length < 8) { return NextResponse.json( { error: "Şifre en az 8 karakter olmalıdır" }, { status: 400 } ); } // Better Auth ile kullanıcı oluştur try { const signUpResponse = await auth.api.signUpEmail({ body: { email, password, name, }, }); if (!signUpResponse || !signUpResponse.user) { throw new Error("Kullanıcı oluşturulamadı"); } const user = signUpResponse.user; // JWT token oluştur const accessToken = signJWT( { userId: user.id, email: user.email, type: "access", }, "7d" ); return NextResponse.json({ success: true, message: "Kayıt başarılı", data: { user: { id: user.id, email: user.email, name: user.name, }, accessToken, }, }); } catch (authError: any) { // Better Auth hatası - muhtemelen email zaten kullanımda if (authError.message?.includes("exists") || authError.message?.includes("duplicate")) { return NextResponse.json( { error: "Bu email adresi zaten kullanımda" }, { status: 409 } ); } throw authError; } } catch (error: any) { console.error("Register API hatası:", error); return NextResponse.json( { error: error.message || "Kayıt sırasında bir hata oluştu" }, { status: 500 } ); } }