I am trying to implement an auto refresh for access-token every "t" 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.
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.
If there is any other way to implement JWT refresh tokens at set intervals of time, I am open to them too.
useEffect(() => {
async function getRefreshToken() {
const response = await axios.post("http://localhost:3001/refreshToken", {refreshToken}, {withCredentials: true})
if (response.data.redirect) {
clearInterval(refresh)
history.replace(`${response.data.redirect}`)
localStorage.clear()
}}
const refresh = setInterval(getRefreshToken, 4000)
}, [])