1
votes
const User = require ('../models/User');
const jwt = require ('jsonwebtoken');
const asynchronous = require('./async');
const ErrorHandler = require ('..utils/errorHandler');

exports.protect = asynchronous(async (res,req,next)=> {
    if(req.headers.authorization && 
        req.headers.authorization.startsWith('Bearer') ) {
      let token = req.headers.authorization.split(' ')[1] 
    }

    if(!token) { 
        return next(new ErrorHandler('Unauthorized,401'))
    }
    try {
      const tokenVerify = jwt.verify(token,process.env.SECRET)
      req.user = await User.findById(decoded.id);
      next(); 
    } catch(err) {
       return next(new ErrorHandler('Unauthorized,401'))
    }
})

Hello good people please can you tell me why anytime I execute the above code in Postman,I get a typeError:cannot read property 'authorization' of undefined? I have require protect in all the necessary routers

2

2 Answers

1
votes

It's simple you have invert req and res ;)

req is before res in the prototype

0
votes

the order would be (req,res) rather than (rea,req).