Connecting Auth0 to MongoDB

Posting this information everywhere that I see Node 18 and people rolling back to 16.

Auth0 Engineering replied to my inquiry and responded with this:

To use version 5 of the mongo driver, you should update your script to use promises. Here’s an example get_user script you can reference.

function getByEmail(email, callback) {
  const MongoClient = require('mongodb').MongoClient;
  const client = new MongoClient(configuration.URL);
  
  (async () => {
    try {
      await client.connect();
      
      const db = client.db(configuration.DB);
      const users = db.collection('users');
      
      const user = await users.findOne({ email: email });
      if (!user) return callback(null, null);
      
      return callback(null, {
        user_id: user._id.toString(),
        nickname: user.nickname,
        email: user.email
      });
    } catch(e) {
      return callback(e);
    } finally {
      await client.close();
    }
  })();
}

I can confirm this works and fixes the problem. I’m able to use latest Mongo 5.x client and Node 18.