Authorization is undefined resulting undefined even after adding token in header

Hi, i am new to postman. I am adding bearer token in authorization during the http request but on server side while reading, i am getting it undefined.

here is the implementation:

const jwt = require('jsonwebtoken');
const jwtAuthMiddleware  = (req, res, next)=>{
    //check header for authorization:
    const auth = req.header.authorization;
    if(!auth) return  res.status(401).json({error: 'token not found here'});
//Extract the jwt token from the request
const token = req.header.authorization.split(' ')[1];
if(!token) return res.status(401).json({error: 'Unauthorized'});
try{
    //verify JWT token
    const decoded = jwt.verify(token, process.env.JWT_SECRET);
    //attaching a key in request as user with value as decoded which is the jwt token payload received after verification
    req.user = decoded;
    next();

}catch(err){
    //throw err;
    res.status(401).json({error: 'Unauthorized'});
}

}

Hey @rchaudhary18 :wave:

Welcome to the Postman Community! :postman:

What is the console output of this?

I would expect that token value in the request, to have Bearer and a space in-front of it.

For example:

Authorization: Bearer abc1234

If the Bearer keyword or the space is missing, the .split(' ')[1] statement would fail to extract the token correctly. I haven’t run your code so I wouldn’t know for sure. :grimacing: