Skip to content

Instantly share code, notes, and snippets.

@wheresvic
Created October 24, 2016 20:08
Show Gist options
  • Save wheresvic/741f853fa6c04b1d51b71fc455d355b0 to your computer and use it in GitHub Desktop.
Save wheresvic/741f853fa6c04b1d51b71fc455d355b0 to your computer and use it in GitHub Desktop.
var authenticateUser = function(req, res, next) {
User.findOne({ username: req.body.username })
.bind({})
.then(function(user) {
this.user = user;
if (user) {
return user.verifyPassword(req.body.password);
}
return Promise.resolve(false);
})
.then(function(isPasswordMatch) {
if (isPasswordMatch) {
return issueToken(this.user._id);
}
return Promise.resolve(null);
})
.then(function(token) {
if (token) {
return res.status(200).send(token);
}
res.status(401).send();
})
.catch(function(err) {
next(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment