0
votes

I'm trying to locally connect to an authentication db to access my app db. If I include the useNewUrlParser: true option it breaks the whole thing with an authentication failed. Removing the useNewUrlParser option connects but doesn't tick this option:

config = {
    database: "mongodb://localhost:27017/authapp",
    auth: {
        user : "admin",
        password : "123456",
        authdb : "admin"
    }

...

mongoose
  .connect(config.database, { 
        auth: config.auth,
        useNewUrlParser: true 
    })
  .then(() => {...}

I get:

[nodemon] starting node index.js (node:17814) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. App is running on 4000 { database_error: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoError: Authentication failed. at Function._getError (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:141:14) at /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:191:31 at _callback (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:320:5) at Connection.messageHandler (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:349:5) at Connection.emit (events.js:321:20) at processMessage (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:384:10) at Socket. (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:553:15) at Socket.emit (events.js:321:20) at addChunk (_stream_readable.js:305:12) at readableAddChunk (_stream_readable.js:280:11) at Socket.Readable.push (_stream_readable.js:214:10) at TCP.onStreamRead (internal/stream_base_commons.js:186:23) { name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }] at Pool. (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/topologies/server.js:433:11) at Pool.emit (events.js:321:20) at /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/pool.js:577:14 at /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/pool.js:1007:11 at callback (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:93:5) at /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:367:21 at /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11 at /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:193:16 at _callback (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:320:5) at Connection.messageHandler (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:349:5) at Connection.emit (events.js:321:20) at processMessage (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:384:10) at Socket. (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:553:15) at Socket.emit (events.js:321:20) at addChunk (_stream_readable.js:305:12) at readableAddChunk (_stream_readable.js:280:11) { name: 'MongoNetworkError', [Symbol(mongoErrorContextSymbol)]: {} } }

Is there a pattern that I am missing or what is a way that works for this case? I also tried the user+pw in the db connection string but it doesn't connect as expected either - maybe missing the authentication db name?

Hope someone can point me in the right direction for this simple issue.

1

1 Answers

0
votes

Use authSource instead of authdb

 auth:{
     user: "admin",
     password: "123456",
     authSource: "admin",                                 
    },