59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import NextAuth, { DefaultSession } from 'next-auth'
|
|
import { PrismaAdapter } from '@auth/prisma-adapter'
|
|
import Credentials from 'next-auth/providers/credentials'
|
|
import bcrypt from 'bcryptjs'
|
|
import { prisma } from '@/lib/db'
|
|
|
|
// NextAuth type augmentation — session.user.role için
|
|
declare module 'next-auth' {
|
|
interface Session {
|
|
user: {
|
|
role?: string
|
|
} & DefaultSession['user']
|
|
}
|
|
}
|
|
|
|
declare module 'next-auth/jwt' {
|
|
interface JWT {
|
|
role?: string
|
|
}
|
|
}
|
|
|
|
export const { handlers, auth, signIn, signOut } = NextAuth({
|
|
adapter: PrismaAdapter(prisma),
|
|
providers: [
|
|
Credentials({
|
|
credentials: {
|
|
email: { label: 'Email', type: 'email' },
|
|
password: { label: 'Password', type: 'password' },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials?.email || !credentials?.password) return null
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: credentials.email as string },
|
|
})
|
|
if (!user?.password) return null
|
|
const valid = await bcrypt.compare(credentials.password as string, user.password)
|
|
if (!valid) return null
|
|
return { id: user.id, email: user.email, name: user.name, role: user.role }
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
jwt({ token, user }) {
|
|
if (user) token.role = (user as { role: string }).role
|
|
return token
|
|
},
|
|
session({ session, token }) {
|
|
if (token && session.user) {
|
|
session.user.role = token.role
|
|
}
|
|
return session
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: '/tr/admin/login',
|
|
},
|
|
session: { strategy: 'jwt' },
|
|
})
|