41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
'use server'
|
||
|
||
import { login } from '@/lib/auth'
|
||
import prisma from '@/lib/prisma'
|
||
import bcrypt from 'bcryptjs'
|
||
import { redirect } from 'next/navigation'
|
||
|
||
export async function authenticate(prevState: any, formData: FormData) {
|
||
const username = formData.get('username') as string
|
||
const password = formData.get('password') as string
|
||
|
||
if (!username || !password) {
|
||
return { error: 'Lütfen tüm alanları doldurun.' }
|
||
}
|
||
|
||
try {
|
||
// Check user in database
|
||
const user = await prisma.users.findUnique({
|
||
where: { username }
|
||
})
|
||
|
||
if (!user) {
|
||
return { error: 'Kullanıcı adı veya şifre hatalı.' }
|
||
}
|
||
|
||
const passwordsMatch = await bcrypt.compare(password, user.password_hash)
|
||
|
||
if (!passwordsMatch) {
|
||
return { error: 'Kullanıcı adı veya şifre hatalı.' }
|
||
}
|
||
|
||
await login(username)
|
||
} catch (error) {
|
||
console.error('Login error:', error)
|
||
return { error: 'Bir hata oluştu, lütfen tekrar deneyin.' }
|
||
}
|
||
|
||
// Redirect to admin dashboard after successful login
|
||
redirect('/admin')
|
||
}
|