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