Skip to content
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

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

Open
domluther opened this issue Aug 27, 2024 · 0 comments
Open

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

domluther opened this issue Aug 27, 2024 · 0 comments

Comments

@domluther
Copy link

domluther commented Aug 27, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant