2

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?

davidesp
  • 3,743
  • 10
  • 39
  • 77

2 Answers2

0

Your 1st and 2nd question are linked. This is the middleware pattern used by node specifically express in this case.This arcticle explains this very nicely IMO.

In my understanding and simple terms, they are functions which pass around the req, res objects (info here), by adding to them with their own changes.

enter image description here

In your code you requireLoginLocal will add to the response object whatever it needs and calls next, in which case node will pass the response object to authController.login and so on. When all the middleware in the chain have done their necessary changes then the final response is send as http response

As for your 2nd question, it's probably used by passport for configuration. The actual middleware for passport middleware is provide by this line

passport.authenticate('local', { session: false });

As this a deep and underlying base topic for node, you can google for node middleware and can find tons of articles out there about this

Community
  • 1
  • 1
Shyam Babu
  • 1,069
  • 7
  • 14
-2

Answers to your questions:

  1. Refer to the official express documentation.
  2. This is the main functionality of passportjs, it acts as an authentication middleware, refer to the passport-local documentation.
  3. There can be multiple callbacks for a single route, but only when it is dependent on the previous callback. Also, any of the route callback can terminate the whole transferring the control to the next callback flow by sending the response with a status to the client.
Abhishek Singh
  • 2,642
  • 15
  • 24