jaredhanson / passport-local

Username and password authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-local/?utm_source=github&utm_medium=referral&utm_campaign=passport-local&utm_content=about
MIT License
2.73k stars 498 forks source link

Configure Strategy the "return" is Redundant #187

Open JohnJiang900526 opened 4 years ago

JohnJiang900526 commented 4 years ago
// example 
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);
    });
  }
));
//  In fact, removing return is also effective, and adding return will also cause misunderstanding
passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { done(err); }
      if (!user) { done(null, false); }
      if (!user.verifyPassword(password)) { done(null, false); }
      done(null, user);
    });
  }
));
jasonandmonte commented 4 years ago

The return stops the evaluation of the function block. In your example done(null, user) would be called regardless if done was called from the if statements.

Example that illustrates the difference with and without the return: https://stackoverflow.com/a/59619194/7582783