Files
moy-qr/app/admin/login/actions.ts
T

41 lines
1.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.
'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')
}