fix: Resolve Prisma Edge Runtime error in middleware
- 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.
This commit is contained in:
48
auth.ts
48
auth.ts
@@ -1,5 +1,6 @@
|
||||
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"
|
||||
@@ -11,22 +12,7 @@ const loginSchema = z.object({
|
||||
})
|
||||
|
||||
export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
maxAge: 30 * 24 * 60 * 60, // 30 days in seconds
|
||||
updateAge: 24 * 60 * 60, // Update session every 24 hours
|
||||
},
|
||||
cookies: {
|
||||
sessionToken: {
|
||||
name: `${process.env.NODE_ENV === 'production' ? '__Secure-' : ''}next-auth.session-token`,
|
||||
options: {
|
||||
httpOnly: true, // Prevent JavaScript access to cookies (XSS protection)
|
||||
sameSite: 'lax', // CSRF protection - prevents cookie from being sent in cross-site requests
|
||||
path: '/',
|
||||
secure: process.env.NODE_ENV === 'production', // Only send cookie over HTTPS in production
|
||||
},
|
||||
},
|
||||
},
|
||||
...authConfig,
|
||||
providers: [
|
||||
Credentials({
|
||||
name: "credentials",
|
||||
@@ -69,34 +55,4 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user, trigger }) {
|
||||
// Add user ID to token on sign in
|
||||
if (user) {
|
||||
token.id = user.id
|
||||
}
|
||||
|
||||
// Handle session expiration
|
||||
// Token will be automatically invalidated by NextAuth when it expires
|
||||
// based on the maxAge setting in session config
|
||||
|
||||
return token
|
||||
},
|
||||
async session({ session, token }) {
|
||||
// Add user ID to session from token
|
||||
if (session.user && token.id) {
|
||||
session.user.id = token.id as string
|
||||
}
|
||||
|
||||
// Session validation happens automatically via JWT expiration
|
||||
// If token is expired, this callback won't be called and user will be redirected
|
||||
|
||||
return session
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: "/auth/login",
|
||||
signOut: "/auth/logout",
|
||||
error: "/auth/error",
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user