如何获得passport.authenticate本地策略使用async/await模式

Kyl*_*son 7 node.js async-await passport-local passport.js

我一直未能获得passport.authenticate在async/await或promise模式下工作.这是一个我认为应该工作的例子,但它无法执行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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何智慧的圣言都会受到高度赞赏.

Kyl*_*son 9

只是让任何其他疲惫的程序员遇到这个..

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

  • `req` 和 `res` 不应该传递给 `promisifiedPassportAuthentication` 吗? (3认同)