44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import NextAuth 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'
|
|
|
|
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 as string
|
|
}
|
|
return session
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: '/tr/admin/login',
|
|
},
|
|
session: { strategy: 'jwt' },
|
|
})
|