Created
September 9, 2017 06:29
-
-
Save nguyentrithuc/922360973f8da8cff99cefbb66ba5d0a 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
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