title: handling jwt token through express returns error "const token = req.cookies.auth; [0] ^ [0] [0] ReferenceError: req is not defined"
body: <p>I am trying to complete jwt sign in I am trying to create the jwt token in login.Then I am trying to use it within my user-questions route. </p>
  
  <p>I am using a react front end.</p>
  
  <p>Is this the correct way to do so? </p>
  
  <p>I am currently getting error </p>
  
  <pre><code>const token = req.cookies.auth;
  [0]               ^
  [0]
  [0] ReferenceError: req is not defined
  
  </code></pre>
  
  <p>Below is my routes login code which assigns the token once the my sql server return that the values for email and password exist. User-questions tries to use this jwt. and I have also included how the token is verfied in a function</p>
  
  <p>Verfiy users</p>
  
  <pre><code>app.get("/user-questions", verifyToken, function(req, res) {
    app.use(function(req, res, next) {
      // decode token
      if (token) {
        jwt.verify(token, "secret", function(err, token_data) {
          if (err) {
            console.info("token did not work");
            return res.status(403).send("Error");
          } else {
            req.user_data = token_data;
            sql.connect(config, function(err) {
              if (err) console.log(err);
  
              // create Request object
              var request = new sql.Request();
  
              // query to the database and get the records
              request.execute("dbo.ViewQuestions", function(err, recordset) {
                if (err) console.log(err);
  
                // send records as a response
  
                res.json(recordset);
                next();
              });
            });
          }
        });
      } else {
        console.info("no token");
        console.log("no token");
        return res.status(403).send("No token");
      }
    });
  });
  
  </code></pre>
  
  <p>Login route</p>
  
  <pre><code>app.post("/login", async (req, response) =&gt; {
    try {
      await sql.connect(config);
  
      var request = new sql.Request();
      var Email = req.body.email;
      var Password = req.body.password;
  
      console.log({ Email, Password });
  
      request.input("Email", sql.VarChar, Email);
      request.input("Password", sql.VarChar, Password);
  
      var queryString =
        "SELECT * FROM TestLogin WHERE email = @Email AND password = @Password";
  
      //"SELECT * FROM RegisteredUsers WHERE email = @Email AND Password = HASHBYTES('SHA2_512', @Password + 'skrrt')";
  
      const result = await request.query(queryString);
  
      if (result.recordsets[0].length &gt; 0) {
        console.info("/login: login successful..");
        console.log(req.body);
  
        token = jwt.sign(
          { Email },
          "secretkey",
          { expiresIn: "30s" },
          (err, token) =&gt; {
            res.json({
              token
            });
            res.cookie("auth", token);
            res.send("ok");
          }
        );
      } else {
        console.info("/login: bad creds");
        response.status(400).send("Incorrect email and/or Password!");
      }
    } catch (err) {
      console.log("Err: ", err);
      response.status(500).send("Check api console.log for the error");
    }
  });
  
  </code></pre>
  
  <p>Verify users </p>
  
  <pre><code>
  // Verify Token
  function verifyToken(req, res, next) {
    // Get auth header value
    const bearerHeader = req.headers["authorization"];
    // Check if bearer is undefined
    if (typeof bearerHeader !== "undefined") {
      // Split at the space
      const bearer = bearerHeader.split(" ");
      // Get token from array
      const bearerToken = bearer[1];
      // Set the token
      req.token = bearerToken;
      // Next middleware
      next();
    } else {
      // Forbidden
      res.sendStatus(403);
    }
  }
  </code></pre>
  
  <p>Please advise if this in theory should work. And if not please advise how to resolve.</p>
  
  <p>EDIT : </p>
  
  <p>The error has been resolved however now simply my jwt tokens to do not work. As when logged in and I manually route to user-questions it does not load the component and within the console it says 403 not available (this is set in the code when the jwt token is not working).</p>
  
  <p>UPDATE: </p>
  
  <p>How would I include  </p>
  
  <pre><code> ['authorization'] = 'Bearer ' + token;
  </code></pre>
  
  <p>into </p>
  
  <pre><code>
   handleSubmit(e) {
      e.preventDefault();
      if (this.state.email.length &lt; 8 || this.state.password.length &lt; 8) {
        alert(`please enter the form correctly `);
      } else {
        const data = { email: this.state.email, password: this.state.password };
  
        fetch("/login", {
          method: "POST", // or 'PUT'
          headers: {
            Accept: "application/json, text/plain, */*",
            "Content-Type": "application/json",
  
          },
          body: JSON.stringify(data)
        })
          // .then(response =&gt; response.json())
          .then(data =&gt; {
            console.log("Success:", data);
  
  
          })
  
          .catch(error =&gt; {
            console.error("Error:", error);
          });
      }
    }
  
  </code></pre>