how to get passport.authenticate local strategy wo

2019-04-09 21:40发布

问题:

I've been failing to get passport.authenticate to work at all inside of an async/await or promise pattern. Here is an example I feel should work, but it fails to execute passport.authenticate().

const passport = require("passport");

let user;

try {
    user = await __promisifiedPassportAuthentication();

    console.log("You'll never have this ", user);
} catch (err) {
    throw err;
}

function __promisifiedPassportAuthentication() {
    return new Promise((resolve, reject) => {

        console.log("I run");

        passport.authenticate('local', (err, user, info) => {

            console.log("I never run");

            if (err) reject(err);
            if (user) resolve(user);
        }
    }
}

Any sage words of wisdom would be greatly appreciated.

回答1:

Just incase any other tired programmer out there encounters this..

function __promisifiedPassportAuthentication() {
    return new Promise((resolve, reject) => {
        passport.authenticate('local', (err, user, info) => {
            ...
        })(req, res) // <-- that guy right there
    }
}