Skip to content

Instantly share code, notes, and snippets.

@bayraktugrul
Created April 17, 2019 12:46
Show Gist options
  • Save bayraktugrul/6bd7291b28ada7b3f42223dae58736b7 to your computer and use it in GitHub Desktop.
Save bayraktugrul/6bd7291b28ada7b3f42223dae58736b7 to your computer and use it in GitHub Desktop.
const jwt = require('jsonwebtoken');
module.exports = (req, res, next) => {
try {
/*JWT is send with request header!
Format of it: Authorization : Bearer <token>
*/
const token = req.headers.authorization.split(" ")[1];
const decodedToken = jwt.verify(token, 'secret_key');
req.userData = decodedToken;
next();
}catch(error) {
return res.status(401).send({
message: 'Auth failed'
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment