Files

45 lines
1.3 KiB
TypeScript
Raw Permalink 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 { prisma } from "@/lib/prisma";
import bcrypt from "bcryptjs";
import { signToken, setAuthCookie, removeAuthCookie } from "@/lib/auth";
import { redirect } from "next/navigation";
export async function login(formData: FormData) {
try {
const email = formData.get("email") as string;
const password = formData.get("password") as string;
if (!email || !password) {
return { success: false, error: "Lütfen email ve şifre giriniz." };
}
const admin = await prisma.admin.findUnique({
where: { email },
});
if (!admin) {
return { success: false, error: "Geçersiz e-posta veya şifre." };
}
const isMatch = await bcrypt.compare(password, admin.password);
if (!isMatch) {
return { success: false, error: "Geçersiz e-posta veya şifre." };
}
// Başarılı giriş, token oluştur ve cookie'ye kaydet
const token = await signToken({ id: admin.id, email: admin.email, name: admin.name });
await setAuthCookie(token);
return { success: true };
} catch (error) {
console.error("Login error:", error);
return { success: false, error: "Giriş yapılırken beklenmeyen bir hata oluştu." };
}
}
export async function logout() {
await removeAuthCookie();
redirect("/admin/login");
}