Skip to content

Instantly share code, notes, and snippets.

@nCally
Created October 20, 2019 16:17
Show Gist options
  • Select an option

  • Save nCally/3a9c82a47805f2b32775fa4212fa87a5 to your computer and use it in GitHub Desktop.

Select an option

Save nCally/3a9c82a47805f2b32775fa4212fa87a5 to your computer and use it in GitHub Desktop.
var jwt = require('jsonwebtoken');
function authorizationController(req, res, next){
var header = req.headers['authorization'];
let token;
if(typeof header !== 'undefined'){
const bearer = header.split(' ');
token = bearer[1];
}
if(!token){
res.status(401).json({ message:"No authorization header"})
} else {
jwt.verify(token, process.env.JWT_SECRET, function(err, decoded){
if(!decoded){ res.status(401).json({ message:"Unauthorized request" }) }
else {
res.phoneNumber = decoded.phoneNumber;
next();
}
})
}
}
module.exports = authorizationController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment