title: how do I redirect user to a page if user is logged in
body: <p>I've tried using useEffect on <code>/dashboard</code> to check if user is loggedIn, but it display dashboard messages before redirecting user back into <code>/login</code> page.</p>
  <p><code>/dashboard</code></p>
  <pre><code>function Dashboard() {
      return (
          &lt;div&gt;
              This is Dashboard page.
          &lt;/div&gt;
      );
  
  }
  
  export default Dashboard;
  </code></pre>
  <p><code>/login</code></p>
  <pre><code>   const login = (e) =&gt; {
          e.preventDefault();
          Axios.post('http://localhost:3001/login', {
              username: username,
              password: password,
          }).then((response) =&gt; {
              if (response.data.message) {
                  setLoginStatus(response.data.message)
              } else {
                  setLoginStatus(response.data[0].username)
              }
          });
      };
  
      useEffect(() =&gt; {
          Axios.get(&quot;http://localhost:3001/login&quot;).then((response) =&gt; {
              if (response.data.loggedIn === true) {
                  setLoginStatus(response.data.user[0].username);
                  history.push(&quot;/dashboard&quot;);
              }
          })
      }, [])
  </code></pre>
  <p><code>server.js</code></p>
  <pre><code>app.get(&quot;/login&quot;, (req, res) =&gt; {
      if (req.session.user) {
          res.send({ loggedIn: true, user: req.session.user })
      } else {
          res.send({ loggedIn: false })
      }
  })
  
  app.post(&quot;/login&quot;, (req, res) =&gt; {
  
      const username = req.body.username;
      const password = req.body.password;
  
      db.query(&quot;SELECT * FROM user WHERE username = ?;&quot;, [username],
          (err, result) =&gt; {
              if (err) {
                  res.send({ err: err });
              }
  
              if (result.length &gt; 0) {
                  bcrypt.compare(password, result[0].password, (err, response) =&gt; {
                      if (response) {
                          req.session.user = result;
                          console.log(req.session.user)
                          res.send(result)
                      } else {
                          res.send({ message: &quot;Wrong username/password!&quot; });
                      }
                  })
              } else {
                  res.send({ message: &quot;User doesn't exist&quot; });
              }
          }
      );
  });
  </code></pre>