title: node js cant post unauthorised
body: <p>i have been facing this error of 401 unauthorized error when i tried to mount my isLoggedinMiddlware.js, and even when i can manage to print the token stored, it stil says its
  authorised. Any advice or help would be appreciated! Have a nice day.</p>
  
  <p>this is my isLoggedinMiddleware.js</p>
  
  <pre><code>const jwt = require("jsonwebtoken");
  const JWT_SECRET = process.env.JWT_SECRET;
  
  module.exports = (req, res, next) =&gt; {
    const authHeader = req.headers.authorization;
    if (authHeader === null || authHeader === undefined || !authHeader.startsWith("Bearer ")) {
      res.status(401).send();
      return;
    }
    const token = authHeader.replace("Bearer ", "");
    jwt.verify(token, JWT_SECRET, { algorithms: ["HS256"] }, (error, decodedToken) =&gt; {
      if (error) {
        res.status(401).send();
        return;
      }
      req.decodedToken = decodedToken;
      next();
    });
  };
  </code></pre>
  
  <p>this is my post api</p>
  
  <pre><code>app.post("/listings/",isLoggedInMiddleware,(req,res)=&gt;{
    listings.insert(req.body,(error,result)=&gt;{
      if(error){
        console.log(error)
        console.log(req.body)
        console.log(isLoggedInMiddleware)
        res.status(500).send('Internal Server Error')
        return;
      }
      console.log(result)
      res.status(201).send({"Listing Id":result.insertId})
    })
  })
  </code></pre>
  
  <p>This is my front end</p>
  
  <pre><code>   const baseUrl = "http://localhost:3000";
  const loggedInUserID = parseInt(localStorage.getItem("loggedInUserID"));
  const token = localStorage.getItem("token")
  console.log(token)
  
  if(token === null || isNaN(loggedInUserID)){
      window.location.href = "/login/"
  }else{
  
      $('#logoff').click(function(){
      event.preventDefault();
      localStorage.removeItem('token')
      localStorage.removeItem('loggedInUserID')
      window.alert('Logging out now')
      window.location.href = "/login/"
      })
           $(document).ready(function () {            
          $('#submitbtn').click((event) =&gt; {
              const loggedInUserID = parseInt(localStorage.getItem("loggedInUserID"));
              // middleware =  {headers:{'Authorization':'Bearer '+token},data:{id: loggedInUserID}}
  
              event.preventDefault();
              const itemName = $("#itemName").val();
              const itemDescription = $("#itemDescription").val();
              const price = $('#price').val();
              const image = $('#image').val();
              const requestBody = {
                  itemName: itemName,
                  itemDescription: itemDescription,
                  price: price,
                  fk_poster_id: loggedInUserID,
                  imageUrl: image
              }
              console.log(requestBody);
              axios.post(`${baseUrl}/listings/`,{headers:{'Authorization':'Bearer '+token},data:{id: loggedInUserID}}, requestBody)
                  .then((response) =&gt; {
                      window.alert("successfully Created")
                  })
                  .catch((error) =&gt; {
                      window.alert("Error")
                      console.log(requestBody)
  
                  })
          })
      })  
  
  }
  </code></pre>
  
  <p>i can managed to get the token i stored when i log in, however, it stills say 401 unauthorised.</p>