Created
October 20, 2019 16:17
-
-
Save nCally/3a9c82a47805f2b32775fa4212fa87a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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