87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
import NextAuth, { AuthOptions } from 'next-auth'
|
||
import CredentialsProvider from 'next-auth/providers/credentials'
|
||
import { compare } from 'bcryptjs'
|
||
import { prisma } from '../../../../lib/prisma'
|
||
import { UserRole } from '@prisma/client'
|
||
|
||
export const authOptions: AuthOptions = {
|
||
providers: [
|
||
CredentialsProvider({
|
||
id: 'credentials',
|
||
name: 'Credentials',
|
||
credentials: {
|
||
email: { label: 'Email', type: 'email' },
|
||
password: { label: 'Şifre', type: 'password' }
|
||
},
|
||
async authorize(credentials) {
|
||
try {
|
||
if (!credentials?.email || !credentials?.password) {
|
||
return null
|
||
}
|
||
|
||
const user = await prisma.user.findUnique({
|
||
where: {
|
||
email: credentials.email
|
||
}
|
||
})
|
||
|
||
if (!user || !user.password) {
|
||
console.error('Kullanıcı bulunamadı:', credentials.email)
|
||
return null
|
||
}
|
||
|
||
const isValid = await compare(credentials.password, user.password)
|
||
|
||
if (!isValid) {
|
||
console.error('Geçersiz şifre:', credentials.email)
|
||
return null
|
||
}
|
||
|
||
console.log('Başarılı giriş:', user.email)
|
||
|
||
return {
|
||
id: user.id,
|
||
email: user.email,
|
||
name: user.name,
|
||
role: user.role
|
||
}
|
||
} catch (error) {
|
||
console.error('Giriş hatası:', error)
|
||
return null
|
||
}
|
||
}
|
||
})
|
||
],
|
||
secret: process.env.NEXTAUTH_SECRET,
|
||
session: {
|
||
strategy: 'jwt',
|
||
maxAge: 30 * 24 * 60 * 60, // 30 gün
|
||
},
|
||
pages: {
|
||
signIn: '/auth/login',
|
||
signOut: '/auth/logout',
|
||
error: '/auth/error'
|
||
},
|
||
callbacks: {
|
||
async jwt({ token, user }) {
|
||
if (user) {
|
||
token.role = user.role
|
||
token.id = user.id
|
||
}
|
||
return token
|
||
},
|
||
async session({ session, token }) {
|
||
if (session?.user) {
|
||
session.user.role = token.role as UserRole
|
||
session.user.id = token.id as string
|
||
}
|
||
return session
|
||
}
|
||
},
|
||
debug: process.env.NODE_ENV === 'development',
|
||
trustHost: true
|
||
}
|
||
|
||
const handler = NextAuth(authOptions)
|
||
|
||
export { handler as GET, handler as POST }
|