Skip to content

Instantly share code, notes, and snippets.

@nguyentrithuc
Created September 9, 2017 06:29
Show Gist options
  • Save nguyentrithuc/922360973f8da8cff99cefbb66ba5d0a to your computer and use it in GitHub Desktop.
Save nguyentrithuc/922360973f8da8cff99cefbb66ba5d0a to your computer and use it in GitHub Desktop.
router.post('/signin', function(req, res) {
User.findOne({
username: req.body.username
}, function(err, user) {
if (err) throw err;
if (!user) {
res.status(401).send({success: false, msg: 'Authentication failed. User not found.'});
} else {
// check if password matches
user.comparePassword(req.body.password, function (err, isMatch) {
if (isMatch && !err) {
// if user is found and password is right create a token
var token = jwt.sign(user, config.secret);
// return the information including token as JSON
res.json({success: true, token: 'JWT ' + token});
} else {
res.status(401).send({success: false, msg: 'Authentication failed. Wrong password.'});
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment