Skip to content

Instantly share code, notes, and snippets.

@chanakaDe
Created October 19, 2015 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanakaDe/bb84dde83b2661279e0f to your computer and use it in GitHub Desktop.
Save chanakaDe/bb84dde83b2661279e0f to your computer and use it in GitHub Desktop.
/**
* Check logged status in order to
* give permission to following links.
*/
api.use(function (req, res, next) {
console.log("Somebody logged into system");
var token = req.body.token || req.param('token') || req.headers['x-access-token'];
// Check if token exists.
if (token) {
jsonwebtoken.verify(token, secretKey, function (err, decoded) {
if (err) {
res.status(403).send({success: false, message: "Failed to authenticate"});
} else {
req.decoded = decoded;
next();
}
});
} else {
res.status(403).send({success: false, message: "No valid token provided"});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment