Created
October 24, 2016 20:08
-
-
Save wheresvic/741f853fa6c04b1d51b71fc455d355b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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