title: How can I refresh JWT token every "t" minutes?
body: <p>I am trying to implement an auto refresh for access-token every &quot;t&quot; minutes; I implemented a setInterval in useEffect (in the root component), which sends a request for a new access-token. When I run the code, at times the interval begins onMount and other times it does not run at all.</p>
  <p>Another problem I am having that when I redirect the user to the login page,  I try to clear the interval and it continues running even after clearInterval.</p>
  <p>If there is any other way to implement JWT refresh tokens at set intervals of time, I am open to them too.</p>
  <pre><code> useEffect(() =&gt; {
        async function getRefreshToken() {
          const response = await axios.post(&quot;http://localhost:3001/refreshToken&quot;, {refreshToken}, {withCredentials: true})
          if (response.data.redirect) {
            clearInterval(refresh)
            history.replace(`${response.data.redirect}`)
            localStorage.clear()
          }} 
  
        const refresh = setInterval(getRefreshToken, 4000)
      }, [])
  </code></pre>