I have the following 3 fragments of code taken from a working project I downloaded from internet:
file: ./routes.js
// ...
var passport = require('passport');
var requireLoginLocal = passport.authenticate('local', { session: false });
var authController = require('./controllers/authController');
// ...
module.exports = function(app) {
// ...
authRoutes.post('/login/local', requireLoginLocal, authController.login);
// ...
}
file: ./config/passport.js
// ...
var LocalStrategy = require('passport-local').Strategy;
// ...
module.exports = function(passport) {
passport.use(new LocalStrategy({
usernameField: 'email' /* changing default */
},
function(email, password, done) {
email = email.toLowerCase();
User.findOne({
'email': email
},
function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { error: 'Login failed. Please try again.' });
}
user.comparePassword(password, function(err, isMatch) {
if (err) {
return done(err);
}
if (!isMatch) {
return done(null, false, { error: 'Login failed. Please try again.' });
}
return done(null, user);
});
}
);
}
));
};
file: ./controllers/authController.js
// ...
exports.login = function(req, res, next) {
var userInfo = getUserInfo(req.user);
res.status(200).json({
token: 'JWT ' + generateToken(userInfo),
user: userInfo
});
}
// ...
I have the following questions:
1- On the file: ./routes.js
, on the line:
authRoutes.post('/login/local', requireLoginLocal, authController.login);
how the data is passed from the 2nd to the 3rd argument?
2- On the file: ./config/passport.js
, how affect those return values to the values passed from the 2nd to the 3rd argument on the line above?
3- On file: ./controllers/authController.js
, on function: exports.login
, nothing is returned there, so, how that affects the values passed from the 3rd argument and a possible 4th argument on an hypothetical line like the one above?