skip to Main Content

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


  1. Try like this;

    const token = req.header.Authorization.split(" ")[1];
    

    and check if you are passing the token in the header correctly, it should be like this;

    headers: {
            'Authorization': 'Bearer YOUR-TOKEN-HERE',
        },
    
    Login or Signup to reply.
  2. req.headers should be used instead of req.header.

    const [type,token] = req.headers.Authorization.split(" ");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search