From 774f577edda93cb9a466c12f8030cdbd7a4447af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Wed, 22 Nov 2017 20:54:20 +0100 Subject: [PATCH] =?UTF-8?q?Ruta=20de=20validaci=C3=B3n=20de=20token=20en?= =?UTF-8?q?=20servicio=20Auth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/auth/server/boot/routes.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/services/auth/server/boot/routes.js b/services/auth/server/boot/routes.js index f29e648f9..9aa76896b 100644 --- a/services/auth/server/boot/routes.js +++ b/services/auth/server/boot/routes.js @@ -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); + }); + } };