authorize.js 689 B

1234567891011121314151617181920212223242526272829303132333435
  1. const jwt = require("jsonwebtoken");
  2. const config = require("../configs/config.json");
  3. const verifyToken = (req, res, next) => {
  4. const token =
  5. req.body.token || req.query.token || req.headers["token"];
  6. if (!token) {
  7. return res.send({
  8. status:'tokenfalse',
  9. data:[{
  10. message: "A token is required for authentication"
  11. }],
  12. error:""
  13. });
  14. }
  15. try {
  16. const decoded = jwt.verify(token, config.token_header_key);
  17. req.user = decoded;
  18. } catch (err) {
  19. return res.send({
  20. status: 'tokenfalse',
  21. data:[{
  22. message: "Invalid Token"
  23. }],
  24. error:""
  25. });
  26. }
  27. return next();
  28. };
  29. module.exports = verifyToken;