verify Toekn error in node js i send token to post man but error found show in postman
verify token are not verify in postman send error 401
const jwtEmail=req.body.email.split("@")[0]
console.log(jwtEmail)
const token=jwt.sign({user_data:email},jwtEmail,{
expiresIn:"24h"
}
)
const verifyToken=async(req,res,next)=>{
try {
const token = req.header.authorization.split("")[0];
const user_data =await User.findOne({auth_token:token})
const jwtEmail = user_data.email.split("@")[0];
const decodedToken = jwt.verify(token,jwtEmail);
next();
} catch (error) {
return res.status(401).send({
message: "Auth failed",
error
});
}
}
TypeError: Cannot read properties of undefined (reading 'split')
2
Answers
Try like this;
and check if you are passing the token in the header correctly, it should be like this;
req.headers
should be used instead of req.header.