- Move all auth configuration to auth.config.ts to avoid Prisma imports in Edge Runtime - Update middleware to use NextAuth with edge-compatible config - Consolidate session, cookies, and callback configuration in auth.config.ts - Update auth.ts to extend authConfig and only add Prisma-dependent providers - This fixes the 'PrismaClient is not configured to run in Edge Runtime' error The middleware now properly works with NextAuth v5 beta without trying to load Prisma in the Edge Runtime.
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import NextAuth from "next-auth"
|
|
import Credentials from "next-auth/providers/credentials"
|
|
import { authConfig } from "@/auth.config"
|
|
import { prisma } from "@/lib/db"
|
|
import bcrypt from "bcryptjs"
|
|
import { z } from "zod"
|
|
|
|
// Validation schemas
|
|
const loginSchema = z.object({
|
|
email: z.string().email(),
|
|
password: z.string().min(1),
|
|
})
|
|
|
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
|
...authConfig,
|
|
providers: [
|
|
Credentials({
|
|
name: "credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "email" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
try {
|
|
// Validate credentials format
|
|
const { email, password } = loginSchema.parse(credentials)
|
|
|
|
// Query user from database
|
|
const user = await prisma.user.findUnique({
|
|
where: { email },
|
|
})
|
|
|
|
// Check if user exists
|
|
if (!user || !user.passwordHash) {
|
|
return null
|
|
}
|
|
|
|
// Verify password with bcrypt
|
|
const isPasswordValid = await bcrypt.compare(password, user.passwordHash)
|
|
|
|
if (!isPasswordValid) {
|
|
return null
|
|
}
|
|
|
|
// Return user object (without password hash)
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
}
|
|
} catch (error) {
|
|
console.error("Authorization error:", error)
|
|
return null
|
|
}
|
|
},
|
|
}),
|
|
],
|
|
})
|