Ruta de validación de token en servicio Auth

This commit is contained in:
Joan Sanchez 2017-11-22 20:54:20 +01:00
parent 52cbd1a870
commit 774f577edd
1 changed files with 25 additions and 1 deletions

View File

@ -85,8 +85,32 @@ module.exports = function(app) {
});
app.get('/logout', function(req, res) {
console.log(req.accessToken);
User.logout(req.accessToken.id,
() => res.redirect('/'));
});
app.get('/validateToken', function(req, res) {
let token = req.headers.authorization;
validateToken(token, function(isValid) {
if (isValid) {
res.status(200);
} else {
res.status(401).json({
message: 'Invalid token'
});
}
});
});
function validateToken(tokenId, cb) {
app.models.AccessToken.findById(tokenId, function(err, token) {
if (token) {
token.validate(function (err, isValid) {
cb(isValid === true, token);
});
} else
cb(false);
});
}
};