feat: Enhance authentication error handling and secret validation

- Added validation for NEXTAUTH_SECRET to ensure it is set before authentication.
- Wrapped the authorization logic in a try-catch block to handle potential errors gracefully and log them for debugging.
This commit is contained in:
ilia 2026-01-04 09:27:37 -05:00
parent 70c4c6ea9e
commit af2faf8f41

View File

@ -3,6 +3,11 @@ import Credentials from "next-auth/providers/credentials"
import { prisma } from "./prisma"
import bcrypt from "bcryptjs"
const nextAuthSecret = process.env.NEXTAUTH_SECRET
if (!nextAuthSecret) {
throw new Error("NEXTAUTH_SECRET is not set. Define it to enable authentication.")
}
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
Credentials({
@ -12,6 +17,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
try {
if (!credentials?.email || !credentials?.password) {
return null
}
@ -39,6 +45,10 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
name: user.name,
role: user.role,
}
} catch (err) {
console.error("Auth authorize error:", err)
return null
}
}
})
],
@ -64,5 +74,5 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
session: {
strategy: "jwt",
},
secret: process.env.NEXTAUTH_SECRET,
secret: nextAuthSecret,
})