Files
image-apiv2/app/api/v1/auth/register/route.ts
Beyhan Oğur 71eff2d979 first commit
2026-04-26 22:09:32 +03:00

81 lines
2.0 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.
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 }
);
}
}