Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Quiero verificar el token y luego verificar algún rol usando el middleware de verificar el token en el middleware de verificar Role, recibí un error "no se encontró el token de verificación"

Quiero verificar el token y luego verificar algún rol usando el middleware de verificar token en el middleware veygiyRole, recibí un error "no se encuentra veryIfytoken"


 const jwt = require("jsonwebtoken"); const ErrorResponse = require("../utils/ErrorResponse"); exports.verifyToken = (req, res, next) => { if ( !req.headers["authorization"] ) { return next(new ErrorResponse("you are not authorize", 403)); } const [Bearer, token] = req.headers["authorization"].split(" "); if (token === null || Bearer !== "Bearer") return next(new ErrorResponse("Invalid Token", 403)); jwt.verify(token, process.env.JWT_SECRET_KEY, (err, user) => { if (err) return next(new ErrorResponse("Token is not valid", 403)); req.user = user; console.log(user); next(); }); }; exports.verifyUser = (req, res, next) => { verifyToken(req, res, next, () => { if (req.user.id === req.params.id || req.user.role === "admin") { next(); } else { return next(new ErrorResponse("you are not authorize", 403)); } }); };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si no está accediendo a verifyToken y verifyUser en ningún lugar fuera del contexto actual, puede modificar su función de la siguiente manera

 const jwt = require("jsonwebtoken"); const ErrorResponse = require("../utils/ErrorResponse"); const verifyToken = (req, res, next) => { if ( !req.headers["authorization"] ) { return next(new ErrorResponse("you are not authorize", 403)); } const [Bearer, token] = req.headers["authorization"].split(" "); if (token === null || Bearer !== "Bearer") return next(new ErrorResponse("Invalid Token", 403)); jwt.verify(token, process.env.JWT_SECRET_KEY, (err, user) => { if (err) return next(new ErrorResponse("Token is not valid", 403)); req.user = user; console.log(user); next(); }); }; const verifyUser = (req, res, next) => { verifyToken(req, res, next, () => { if (req.user.id === req.params.id || req.user.role === "admin") { next(); } else { return next(new ErrorResponse("you are not authorize", 403)); } }); };

y si desea acceder a estas funciones fuera del contexto actual, lo hace con

 module.exports = { verifyToken, verifyUser }

espero que esto ayude :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!