title: User session not persisting via React hooks
body: <p>I am trying to keep a currently user logged in on refresh via server-side authentication. I am using the UseEffect() function to do this in which I verify on refresh.
  My issue is that whenever I refresh, my server reads a user session on and off. Meaning that one refresh will read no session, while the other refresh will read a user session, and so on.
  I want my app.js to always read code to always read 'auth:true' assuming user is logged in.</p>
  <p>Server-side:</p>
  <hr />
  <p>index.js</p>
  <pre><code>app.use(express.json()); //Parsing Json
  
  app.use(cors({   //Parsing origin of the front-end
     origin: [&quot;http://localhost:3000&quot;], 
     methods: [&quot;GET&quot;, &quot;POST&quot;],
     credentials: true   //Allows cookies to be enabled
  }));  
  
  app.use(cookieParser());
  app.use(bodyParser.urlencoded({ extended: true }));
  
   app.use(
    session({
      key: &quot;userID&quot;,
      secret: &quot;subscribe&quot;,    //Normally this has to be long and complex for security
      resave: false,
      saveUninitialized: false,
      cookie: {  //How long will the cookie live for?
        expires: 60 * 60 * 1000, //Expires after one hour
      }
    }));
  
   app.post('/isUserAuth', (req, res) =&gt; { //Where we Authenticate session 
  
    const token = req.body.token;
  
    jwt.verify(token, &quot;jwtSecret&quot;, (err, decoded) =&gt; {
      if (err) {
      res.send({auth: false, user: &quot;No valid token!&quot;});
      } 
      else if (!req.session.user) {
        res.send({auth: false, user: &quot;empty user&quot;});
        console.log(req.session.user)
      }
      else  { //Else if user is verified, we send confirmed authentication and user data
      res.send({auth: true, user: req.session.user});
      console.log(req.session.user)
      }
  })
  });
  </code></pre>
  <p>Client-side:</p>
  <hr />
  <p>app.js</p>
  <pre><code>const userAuthToken = localStorage.getItem('token'); 
    
    useEffect(() =&gt; { //Stay logged in, if user is logged in, after refresh
      Axios.post(&quot;http://localhost:3001/isUserAuth&quot;, {   //End-point for creation request
      token: userAuthToken
     
      }).then(response =&gt; {
        if (!response.data.auth) { //checking for auth status
          setAuthStatus(false); //User isnt logged in!
          console.log(&quot;NOT LOGGED IN!&quot;);
          console.log(response.data.user);
         } else {
          setAuthStatus(true);      //User is logged into session!
          console.log(&quot;LOGGED IN!&quot;);
          console.log(response.data.user);
         }
      })
    }
    ,[]);
  </code></pre>