Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. const jwt = require("jsonwebtoken");
  2. const config = require("config");
  3.  
  4. module.exports = function(req, res, next) {
  5. //get the token from the header if present
  6. const token = req.headers["x-access-token"] || req.headers["authorization"];
  7. //if no token found, return response (without going to the next middelware)
  8. if (!token) return res.status(401).send("Access denied. No token provided.");
  9.  
  10. try {
  11. //if can verify the token, set req.user and pass to next middleware
  12. const decoded = jwt.verify(token, config.get("myprivatekey"));
  13. req.user = decoded;
  14. next();
  15. } catch (ex) {
  16. //if invalid token
  17. res.status(400).send("Invalid token.");
  18. }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement