user3552178
user3552178

Reputation: 3013

passport conditionally authenticate: Route.post() requires callback functions but got a [object Undefined]

ok i had a piece of code doing login using passport. Now based on different linux distribution i want to do it differently.

function loginDispatch(osType) {
    if(osType == "sles") {
        passport.authenticate('myWorkingAuth');
    }
    else if(osType == "ubuntu") {
        passport.authenticate('local');
    }
    else {  
        console.log("error");
    }
}

app.post('/login', loginDispatch(osInfo), function (req, res, next) {
    next();
}, function(req, res) {
    logger.trace('login called with user = ' + req.user.name);

    //save the user in our session
    req.session.user = req.user;
    // ..............

    res.send(req.session.user);
});

But it gives such error in Ubuntu: Error: Route.post() requires callback functions but got a [object Undefined]

How to fix it please ? Thanks !

Upvotes: 0

Views: 250

Answers (1)

Tom Jardine-McNamara
Tom Jardine-McNamara

Reputation: 2618

You are passing the result of loginDispatch as middleware. loginDispatch therefore needs to return a function. Amend your function to return the appropriate passport middleware:

if(osType == "sles") {
    return passport.authenticate('myWorkingAuth');
}
else if(osType == "ubuntu") {
    return passport.authenticate('local');
}
else {  
    console.log("error");
}

As an aside, you probably want to handle the final else a little more robustly, but I'm assuming this is test code and you know that :)

Upvotes: 1

Related Questions