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

166
Views
I want to verify token and then verify some role using verifytoken middleware in verifyRole middleware, I got an error "verifytoken is not found"

I want to verify token and then verify some role using verifytoken middleware in veygiyRole middleware, I got an error "veryIfytoken is not found"


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

If you are not accessing verifyToken and verifyUser anywhere outside the current context you can modify your function as follows

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));
    }
  });
};

and if you want to access these function outside of the current context you do so with

module.exports = {
     verifyToken,
     verifyUser
}

hope this helps :)

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!