Authentication error when trying to connect to MongoDB atlas via mongoose + node.js

I’m trying to deploy a personal project that’s a full stack application built with React/Node.js/MongoDB. I’m using Firebase to deploy the whole application (Firebase Cloud Functions specifically for the backend). I’ve successfully deployed the front-end and back-end but I’m having issues connecting to my database. When building in development I used mongoose and stored the mongodb data locally. For deployment I’m trying to use MongoDB Atlas to connect to my backend. However, I keep getting an authentication error when I run firebase emulator and then try to make a request to that url via Postman. I AM able to successfully connect with the same credentials using Compass, but not via the node.js driver.

  • I’ve made sure to whitelist all IP addresses

  • I triple-checked my username and password (making sure it’s the one specific to my db user, NOT the general website login) and reset it 3 times just in case.

  • I’ve stored the connection string, with the updated username and password (<> symbols removed) both in firebase functions config and in .runtimeconfig.json file for the emulator testing.

  • I’ve made sure there are no special characters in the password. I’ve restarted everything after updates to ensure the latest changes are applied.

  • I’ve run terminal commands to make sure all the relevant ports are listening (27017 & 4000, 4500, 5001for Firebase)

I’m working in VS code on a Macbook. Here is the error I keep getting:

>  Error connecting to MongoDB Atlas: MongoServerError: bad auth : Authentication failed.
>      at Connection.onMessage (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:202:26)
>      at MessageStream.<anonymous> (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:61:60)
>      at MessageStream.emit (node:events:517:28)
>      at processIncomingData (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:124:16)
>      at MessageStream._write (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
>      at writeOrBuffer (node:internal/streams/writable:392:12)
>      at _write (node:internal/streams/writable:333:10)
>      at Writable.write (node:internal/streams/writable:337:10)
>      at TLSSocket.ondata (node:internal/streams/readable:777:22)
>      at TLSSocket.emit (node:events:517:28) {
>    ok: 0,
>    code: 8000,
>    codeName: 'AtlasError',
>    connectionGeneration: 0,
>    [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
>  }
>  node:internal/process/promises:288
>              triggerUncaughtException(err, true /* fromPromise */);
>              ^
>  
>  MongoServerError: bad auth : Authentication failed.
>      at Connection.onMessage (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:202:26)
>      at MessageStream.<anonymous> (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:61:60)
>      at MessageStream.emit (node:events:517:28)
>      at processIncomingData (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:124:16)
>      at MessageStream._write (/Users/coder/Documents/Code/MVP/1more_page/functions/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
>      at writeOrBuffer (node:internal/streams/writable:392:12)
>      at _write (node:internal/streams/writable:333:10)
>      at Writable.write (node:internal/streams/writable:337:10)
>      at TLSSocket.ondata (node:internal/streams/readable:777:22)
>      at TLSSocket.emit (node:events:517:28) {
>    ok: 0,
>    code: 8000,
>    codeName: 'AtlasError',
>    connectionGeneration: 0,
>    [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
>  }
>  
>  Node.js v18.18.0
i  Request to function failed: Error: socket hang up

I’d greatly appreciate any insight - thank you!