Configure Strategy the "return" is Redundant
JohnJiang900526 opened this issue · 1 comments
JohnJiang900526 commented
// 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
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