Search code examples
expressroutespassport.jspassport-local

Express Passportjs Authenticate not being reached in router callback


If I pass passport.authenticate("local") as middleware into my route, it executes. But this way I do not have access to res so I can send a message back to my front end. However, if I attempt to call it in the route callback function, it is not firing.

router.post("/login", function(req, res, next) {
    passport.authenticate("local", function(err, user, info) {
        console.log("Unreached"); // This is not logging
    });
})

Here is my passport.use inside app.js

passport.use(new LocalStrategy({
        usernameField: "portalId"
    }, function(portalId, enteredPassword, done) {
        var params = {
            TableName: "MyTableName",
            KeyConditionExpression : "PortalID = :portalID",
            ExpressionAttributeValues : {
                ":portalID" : Number(portalId)
            }
        }
        docClient.query(params, function(err, user) {
            if (err) throw err;
            let realPassword = user.Items[0].password;

            bcrypt.compare(enteredPassword, realPassword, function(err, res) {
                if (err) throw err;
                if (res) {
                    return done(null, user);
                }
                if (!res) {
                    return done(null, false, { message: "Invalid Credentials" });
                }
            })
        })
    }));

Solution

  • Saw in some other post a snippet of code using the custom callback and he had (req, res, next) right after the passport.authenticate function. I added this and my code was being fired now.