So I have a a promise chain that solves a certain callback hell I was experiencing.
Here's what the chain looks like:
server.exchange(oauth2orize.exchange.password(
function(client, email, password, scope, done) {
users.findOne({email: email})
.then(authenticateUser) // mix in password here?
.then(deleteExistingTokens)
.then(createAndSaveNewTokens)
.then(function(results){
done(null, results[0], results[1], {'expires_in': tokenLife});
}).catch(err => {done(err);});
}));
So users.findOne returns a promise that returns my user. I need to 'mix in' the password to authenticate against. Given that this is my definition of authenticateUser, how would I go about inserting new variables in the chain?
const authenticateUser = (err, user) => { // add password here?
return Promise((resolve, reject) => {
if (!user) {
reject('User not found');
}
try {
return User(user).authenticate(password)
.then((result) => {
if (result) {
resolve(user);
} else {
reject('Invalid password');
}
});
}
catch (err) {
reject('Invalid user');
}
});
};