import NextAuth from "next-auth" import CredentialsProvider from "next-auth/providers/credentials" import bcrypt from "bcryptjs" import { authConfig } from "./auth.config" import { db } from "./db" export const { handlers, auth, signIn, signOut } = NextAuth({ ...authConfig, providers: [ CredentialsProvider({ name: "Credentials", credentials: { email: { label: "Email", type: "email" }, password: { label: "Password", type: "password" } }, async authorize(credentials) { const email = credentials?.email as string | undefined const password = credentials?.password as string | undefined if (!email || !password) return null const user = await db.user.findUnique({ where: { email } }) if (!user?.password || user.role !== "ADMIN") return null const isValid = await bcrypt.compare(password, user.password) if (!isValid) return null return { id: user.id, name: user.name, email: user.email, role: user.role } } }) ] }) /** Server actions / route handlers should call this before any admin-only mutation. */ export async function requireAdmin() { const session = await auth() if (!session || (session.user as any)?.role !== "ADMIN") { throw new Error("Yetkisiz erişim: Bu işlem için admin girişi gerekli.") } return session }