first commit
This commit is contained in:
73
app/api/v1/auth/login/route.ts
Normal file
73
app/api/v1/auth/login/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
80
app/api/v1/auth/register/route.ts
Normal file
80
app/api/v1/auth/register/route.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user