leonardodavinci
leonardodavinci

Reputation: 1

Some code confusing in MEAN Web Development 2en

exports.signin = function(req, res, next) {
    passport.authenticate('local', function(err, user, info) {
        if (err || !user) {
            res.status(400).send(info);
        } else {
            // Remove sensitive data before user.password = undefined; user.salt = undefined;
            req.login(user, function(err) {
                if (err) {
                    res.status(400).send(err);
                } else {
                    res.json(user);
                }
            });
        }
    })(req, res, next);
};

This a piece of code in the "MEAN Web Development" book by AmosQ.Haviv.Who could tell me what the method passport.authenticate()'tail:(req, res, next) means?Is that a Closure?

Upvotes: -1

Views: 29

Answers (1)

Shilly
Shilly

Reputation: 8589

passport.authenticate() will probably take in the settings 'local' (a domain ? ) and the calllback to create a new function that will do the authentication.

This function will indeed create a closure over 'local' and the callback. The new auth function will expect 3 parameters: the original req(uest), res(ponse) object and a next parameter and is immediately called using the ( req, res, next ) syntax.

So it's very likely that this specific function will do the login and then run the callback used to create the auth function, passing req and res back into the callback.

Look at it as a way to use the passport.authenticate() method to create different versions of logins you can use. One for 'local', one for 'otherDomain', etc.

Upvotes: 1

Related Questions