Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get passport.authenticate local strategy working with async/await pattern

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.

like image 555
Kyle Richardson Avatar asked Feb 22 '17 04:02

Kyle Richardson


1 Answers

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
    }
}
like image 182
Kyle Richardson Avatar answered Oct 18 '22 07:10

Kyle Richardson