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

Configure Strategy the "return" is Redundant #187

Open
JohnJiang900526 opened this issue Jul 10, 2020 · 1 comment
Open

Configure Strategy the "return" is Redundant #187

JohnJiang900526 opened this issue Jul 10, 2020 · 1 comment

Comments

@JohnJiang900526
Copy link

// 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
Copy link

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

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

2 participants