如何获取护照.使用异步/等待模式对本地策略进行身份验证 [英] how to get passport.authenticate local strategy working with async/await pattern

查看:63
本文介绍了如何获取护照.使用异步/等待模式对本地策略进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直未能获得passport.authenticate在异步/等待或承诺模式内的所有工作.这是我认为应该可以使用的示例,但是无法执行passport.authenticate().

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.

推荐答案

以防万一其他疲倦的程序员遇到这种情况.

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
    }
}

这篇关于如何获取护照.使用异步/等待模式对本地策略进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆