Edge Compatibility issue, Nextjs, next-auth v5; #11921
Unanswered
Mohiuddin2
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello buddy,
I am trying to implement auth.js in my nextjs project. The project is in js not ts. Code view:
./db.js
`// This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
import { MongoClient, ServerApiVersion } from 'mongodb'
if (!process.env.MONGODB_URL) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URL
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
}
let client
let clientPromise
if (process.env.NODE_ENV === 'development') {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options)
global._mongoClientPromise = client.connect()
}
clientPromise = global._mongoClientPromise
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options)
clientPromise = client.connect()
}
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise`
___________./nextauth.js-------------------------------------------------
`import NextAuth from 'next-auth'
import GitHub from 'next-auth/providers/github'
import FacebookProvider from 'next-auth/providers/facebook'
import GitHubProvider from 'next-auth/providers/github'
import GoogleProvider from 'next-auth/providers/google'
import Auth0Provider from 'next-auth/providers/auth0'
import authConfig from './authConfig'
import Credentials from 'next-auth/providers/credentials'
import { MongoDBAdapter } from '@auth/mongodb-adapter'
import clientPromise from './db'
// import User from '@/models/User.model'
// import db from '@/app/utils/db'
// db.connectDb()
export const { handlers, auth, signIn, signOut } = NextAuth({
...authConfig,
adapter: MongoDBAdapter(clientPromise),
providers: [
Credentials({
async authorize(credentials) {
console.log('credentials----', credentials, 'Req-------', credentials)
if (credentials === null) return null
],
// debug: true,
callbacks: {
async session({ session, token }) {
// Check if token.sub (user ID) exists, and avoid unnecessary DB queries
if (token.sub) {
let user = await User.findById(token.sub)
console.log('User from atuh.js----', user)
console.log('User from atuh.js----secret', process.env.JWT_SECRET)
session.user.id = token.sub || user._id.toString() // Fixed typo: toString
session.user.role = user.role || 'user'
}
return session
},
async jwt({ token, user }) {
// Persist the user ID in the token
if (user) {
token.sub = user.id // Persist user ID in the token
}
return token
},
},
// pages: {
// signIn: '/signin',
// },
session: {
strategy: 'jwt',
},
// basePath: '/auth/signin',
secret: process.env.JWT_SECRET,
})
`
--------------./authConfig.js
const authConfig = {
session: {
strategy: 'jwt',
maxAge: 24 * 60 * 60, // 1 Day
},
providers: [],
basePath: '/signin',
// basePath: '/auth',
}
export default authConfig
..
The problem is when it connecting mongdb/mongodbclient/adapter it's giving this error:
Import trace for requested module:
./node_modules/gcp-metadata/build/src/index.js
./node_modules/mongodb/lib/deps.js
./node_modules/mongodb/lib/client-side-encryption/client_encryption.js
./node_modules/mongodb/lib/index.js
./node_modules/@auth/mongodb-adapter/index.js
./src/lib/auth.js
./src/components/signIn_signup/SignInForm.js;
Please help me to solve this issue. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions