Skip to content

Document authSource option #8517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
harrisoff opened this issue Jan 17, 2020 · 8 comments
Closed

Document authSource option #8517

harrisoff opened this issue Jan 17, 2020 · 8 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@harrisoff
Copy link

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?
MongoError: Authentication failed. But I could connect to the server with MongoDB Compass using the same username and password.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
function connectDB() {
  const databaseOption = {
    // useNewUrlParser: true,
    // useUnifiedTopology: true,
    auth: { authdb: 'admin', user: 'admin', password: 'mypassword' },
  };
  return new Promise((resolve, reject) => {
    mongoose.connect(
      'mongodb://ip:27017/db',
      databaseOption,
      async (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      },
    );
  });
}

connectDB()
  .then(() => {
    console.log('done');
  })
  .catch((err) => {
    console.error(err);
  });

What is the expected behavior?
Connected to mongodb server.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node v12.13.1
mongoose v5.8.7
mongodb v4.2.2

@harrisoff
Copy link
Author

The following didn't work either, which I believe is basically the sample given in the document.

function connectDB() {
  const databaseOption = {
    useNewUrlParser: true,
  };
  return new Promise((resolve, reject) => {
    mongoose.connect(
      'mongodb://admin:mypassword@ip:27017/db',
      databaseOption,
      async (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      },
    );
  });
}

@Ncifra
Copy link

Ncifra commented Jan 17, 2020

Be sure not to have any special characters in the password that may need escaping. In that case it would be better to provide the password as a parameter inside the above databaseOption object.

@harrisoff
Copy link
Author

Now it works.

function connectDB() {
  const databaseOption = {
    useNewUrlParser: true,
    useUnifiedTopology: true
  };
  return new Promise((resolve, reject) => {
    mongoose.connect(
      'mongodb://admin:mypassword@ip:27017/db?authSource=admin',
      databaseOption,
      async (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      },
    );
  });
}

It seems that the key point is authSource in the query string. But this option is not mentioned in the document. I wonder why other codes did not work.
@Ncifra There are only numbers and normal english letters in my password.

@Ncifra
Copy link

Ncifra commented Jan 17, 2020

That depends though on the type of user you are using, and where it was created. I think that by default it searches for users on the same database you have specified, sort of a database level user scope.

@vkarpov15 vkarpov15 changed the title MongoError: Authentication failed Document authSource option Jan 23, 2020
@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Jan 23, 2020
@vkarpov15 vkarpov15 added this to the 5.8.10 milestone Jan 23, 2020
@vkarpov15
Copy link
Collaborator

We'll add authSource to the docs. Here's some more info on the authSource option.

Also, I hate to be pedantic, but your connectDB() function is pretty clumsy:

return new Promise((resolve, reject) => {
    mongoose.connect(
      'mongodb://admin:mypassword@ip:27017/db?authSource=admin',
      databaseOption,
      async (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      },
    );
  });

Please never make a callback async, that just leads to unhandled promise rejections. Furthermore, mongoose.connect() returns a promise, so there's no need for the additional code gymnastics. All you really need is:

return mongoose.connect(
      'mongodb://admin:mypassword@ip:27017/db?authSource=admin',
      databaseOption);

@harrisoff
Copy link
Author

@vkarpov15
Thanks for your attention.
BTW I didn't mean to be pedantic, this is a fragment I copied from a project.

@harrisoff
Copy link
Author

@vkarpov15
Though it worked with username/password/authSource in the URL, is it a correct way to provide them in the options object?
Error message: Server selection timed out after 30000 ms.
Error reason: password must be a string.

mongoose.connect(
  'mongodb://ip:27017/db',
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    user: 'admin',
    password: 'mypassword'
  }
);

@vkarpov15
Copy link
Collaborator

@harrisoff try pass instead of password as documented here: https://mongoosejs.com/docs/connections.html#options .

I would usually recommend putting username and password in the connection string, that makes it easier to connect to different mongodb instances for dev/prod.

@Automattic Automattic locked as resolved and limited conversation to collaborators Jan 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants