This commit is contained in:
Şahan Hasret
2025-11-21 17:46:30 +03:00
parent c0b7fb463e
commit 76c31274d5
46 changed files with 3675 additions and 1043 deletions

View File

@@ -0,0 +1,73 @@
import { NextRequest, NextResponse } from 'next/server';
import { compare } from 'bcryptjs';
import { prisma } from '@/lib/prisma';
import { encrypt } from '@/lib/auth';
export async function POST(request: NextRequest) {
try {
const { username, password } = await request.json();
if (!username || !password) {
return NextResponse.json(
{ error: 'Kullanıcı adı ve şifre gerekli' },
{ status: 400 }
);
}
// Kullanıcıyı bul
const user = await prisma.user.findUnique({
where: { username },
});
if (!user) {
return NextResponse.json(
{ error: 'Kullanıcı adı veya şifre hatalı' },
{ status: 401 }
);
}
// Şifreyi kontrol et
const isPasswordValid = await compare(password, user.password);
if (!isPasswordValid) {
return NextResponse.json(
{ error: 'Kullanıcı adı veya şifre hatalı' },
{ status: 401 }
);
}
// JWT token oluştur
const token = await encrypt({
userId: user.id.toString(),
username: user.username,
});
// Response oluştur
const response = NextResponse.json(
{
user: {
id: user.id,
username: user.username,
},
},
{ status: 200 }
);
// Cookie'ye token ekle
response.cookies.set('session', token, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 60 * 60 * 24, // 24 saat
path: '/',
});
return response;
} catch (error) {
console.error('Login error:', error);
return NextResponse.json(
{ error: 'Giriş yapılırken bir hata oluştu' },
{ status: 500 }
);
}
}