74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
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 } = body;
|
||
|
||
// Validasyon
|
||
if (!email || !password) {
|
||
return NextResponse.json(
|
||
{ error: "Email ve password gereklidir" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// Better Auth ile giriş yap
|
||
try {
|
||
const signInResponse = await auth.api.signInEmail({
|
||
body: {
|
||
email,
|
||
password,
|
||
},
|
||
});
|
||
|
||
if (!signInResponse || !signInResponse.user) {
|
||
return NextResponse.json(
|
||
{ error: "Geçersiz email veya şifre" },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
|
||
const user = signInResponse.user;
|
||
|
||
// JWT token oluştur
|
||
const accessToken = signJWT(
|
||
{
|
||
userId: user.id,
|
||
email: user.email,
|
||
type: "access",
|
||
},
|
||
"7d"
|
||
);
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: "Giriş başarılı",
|
||
data: {
|
||
user: {
|
||
id: user.id,
|
||
email: user.email,
|
||
name: user.name,
|
||
},
|
||
accessToken,
|
||
},
|
||
});
|
||
} catch (authError: any) {
|
||
// Better Auth hatası - muhtemelen geçersiz credentials
|
||
console.error("Better Auth login hatası:", authError);
|
||
return NextResponse.json(
|
||
{ error: "Geçersiz email veya şifre" },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
} catch (error: any) {
|
||
console.error("Login API hatası:", error);
|
||
return NextResponse.json(
|
||
{ error: "Giriş sırasında bir hata oluştu" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|