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

155
Views
Node Js pasa el rol como una cadena a la función de verificación JWT

la función de verificación JWT acepta req, res y next como sus parámetros. Necesito pasar una cadena adicional 'Admin' para que solo los usuarios administradores puedan acceder a esta API

Mi código jwtVerification.js:

 module.exports = async function (req, res, next) { //I need to be able to add role to this call try { const token = req.header("Authorization"); if (!token) return res.status(401).send('Invalid access token.'); const _token = token.substring(7, token.length); const decoded = jwt.verify(_token, process.env.JWT_PRIVATE_KEY) const user = await prisma.user.findFirst({ where: { id: decoded.id } }); if (!user) return res.status(401).send('Invalid access token.'); //I need to be able to read the role so that I can do the following verifications //if(!role) next(); //else{ // if(user.role !== role || decode.role !== role) return res.status(403).send('Forbidden!') // else next(); //} next(); } catch (error) { res.status(401).send(error.message); } };

finalmente, la API se llama a sí misma: //usa verificarJWT('Admin') por ejemplo

 router.post('/test', verifyJWT, async (req, res) => { res.send('hi'); })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tu gato usa algo como esto:

 module.exports = function (myParam) => { return async function (req, res, next) { //use myParam here try { const token = req.header("Authorization"); if (!token) return res.status(401).send('Invalid access token.'); const _token = token.substring(7, token.length); const decoded = jwt.verify(_token, process.env.JWT_PRIVATE_KEY) const user = await prisma.user.findFirst({ where: { id: decoded.id } }); if (!user) return res.status(401).send('Invalid access token.'); //I need to be able to read the role so that I can do the following verifications //if(!role) next(); //else{ // if(user.role !== role || decode.role !== role) return res.status(403).send('Forbidden!') //} next(); } catch (error) { res.status(401).send(error.message); } } };

Y después de eso, usa el middleware de esta manera:

 router.post('/test', verifyJWT(someParam), async (req, res) => { res.send('hi'); })
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!