Bad auth : Authentication failed. - Nodejs / MongoDB atlas

HI there!!

So Im new to this whole developing world and would be grateful if anyone could help me out.
I’m trying to connect a nodejs app to mongodb(atlas) with mongoose. It was all working fine until I created a new user for the database, and now all my users get bad auth: Authentication failed.
Things I have doubled checked

  • IP adress is set up correctly for my cluster
  • User is atlasAdmin
  • Made sure password and URI is correct

NODEJS APP - index.js
const express = require("express");
const mongoose = require("mongoose");
const app = express();

// Connect to DB
const mongoURI = "mongodb+srv://MyUserName :<MyPassword>@cluster0.lbb8n.mongodb.net/<DbImUsing>?retryWrites=true&w=majority";

mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

// Route
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("index");
});

// Endpoint
app.post("/shortUrls", (req, res) => {});
app.listen(process.env.PORT || 5000);

Error message I am getting is this:

(node:5058) UnhandledPromiseRejectionWarning: MongoError: bad auth : Authentication failed.`
        `    at MessageStream.messageHandler (/Users/fridavbg/Desktop/MovingWorlds/urlShortv2/node_modules/mongodb/lib/cmap/connection.js:268:20)`
        `    at MessageStream.emit (events.js:315:20)`
        `    at processIncomingData (/Users/fridavbg/Desktop/MovingWorlds/urlShortv2/node_modules/mongodb/lib/cmap/message_stream.js:144:12)`
        `    at MessageStream._write (/Users/fridavbg/Desktop/MovingWorlds/urlShortv2/node_modules/mongodb/lib/cmap/message_stream.js:42:5)`
        `    at writeOrBuffer (_stream_writable.js:352:12)`
        `    at MessageStream.Writable.write (_stream_writable.js:303:10)`
        `    at TLSSocket.ondata (_stream_readable.js:719:22)`
        `    at TLSSocket.emit (events.js:315:20)`
        `    at addChunk (_stream_readable.js:309:12)`
        `    at readableAddChunk (_stream_readable.js:284:9)`
        `    at TLSSocket.Readable.push (_stream_readable.js:223:10)`
        `    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)`
        
     (Use node --trace-warnings ... to show where the warning was created)`
     (node:5058) 

    UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see  https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

    (node:5058) [DEP0018] 
    DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.`

If anyone could point me in a direction to solve this, I would be grateful!!

3 Likes

The < and > are not part of a valid URI. It is customary to surround parts that needs to be changed within the less-than and then greater-than signs. It is called a place holder. If, as you write, your user is atlasAdmin then the URI must reflect that rather than having MyUserName. Ditto for the MyPassword. It has to be the real password.

2 Likes

Thanks Steeve!!

Yes, it was the small < >. feel a bit bad that it was so simple. But at least I got a few hours in reading the documentation :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.