Next.js Discord

Discord Forum

Mongo Adapter in AuthJS

Unanswered
German yellowjacket posted this in #help-forum
Open in Discord
German yellowjacketOP
I was reading this part of the AuthJS documentation
https://authjs.dev/getting-started/adapters/mongodb

and I got to this section
The MongoDB adapter does not handle connections automatically, so you will have to make sure that you pass the Adapter a MongoClient that is connected already.

But I am not quite sure what that means. How do I pass the adapter a MongoClient that is connected already. I do have this file.
src/lib/mongodb.js
import mongoose from 'mongoose';

// MongoDB connection
export async function connectToMongoDB() {
   if (!process.env.MONGODB_URI) {
      throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
   }

   try {
      const mongoDBConnection = await mongoose.connect(process.env.MONGODB_URI, {
         useNewUrlParser: true,
         useUnifiedTopology: true,
      });
      console.log('MongoDB connected');
      return mongoDBConnection;
   } catch (error) {
      console.error('Error connecting to MongoDB:', error);
      throw new Error('Error connecting to MongoDB: ' + error.message);
   }
}


I already implemented the rest of the code on that page. Just not sure if there is something else I need to do.

0 Replies