Skip to content

Documentation out of date - findOne no longer accepts a callback #196

@domluther

Description

@domluther

Since mongoose 7, findOne no longer accepts a callback. However, the documentation on both the readme of this github and your passport-local strategy page have this example block of code that tries to use a callback. This then fails.

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

It needs to be re-written to use promises or async await. I changed it to promises here and this works.

  passport.use(
    new LocalStrategy(function (username, password, done) {
      User.findOne({ username: username })
        .then((user) => {
          if (!user) {
            return done(null, false); // No user found with that username
          }

          return user.verifyPassword(password).then((isMatch) => {
            if (!isMatch) {
              return done(null, false); // Password did not match
            }

          });
          return done(null, user); // Successful authentication
        })
        .catch((err) => {
          return done(err); // Error during the process
        });
    })

Environment

  • Operating System: Mac OS
  • Node version: 20.16
  • passport version: 0.7.0
  • passport-local version: 1.0.0
  • mongoose version: 8.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions