javascript - How can I refresh JWT token every "t" minutes? - Stack Overflow

I am trying to implement an auto refresh for access-token every "t" minutes; I implemented a

I am trying to implement an auto refresh for access-token every "t" minutes; I implemented a setInterval in useEffect (in the root ponent), 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)
    }, [])

I am trying to implement an auto refresh for access-token every "t" minutes; I implemented a setInterval in useEffect (in the root ponent), 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)
    }, [])
Share Improve this question asked Sep 15, 2021 at 4:13 ArcanusArcanus 7009 silver badges24 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Here is a working codesandbox example. You can change the functions to match your case

Explanation

What you want is a ref to store the interval to clear it outside of useEffect. You can set the interval just like you do inside useEffect, and save the interval inside the ref:

const ref = useRef()

useEffect(() => {
   const interval = setInterval(...)
   ref.current = interval
}, [])

You need to use a cleanup function inside useEffect to avoid memory leaks:

useEffect(() => {
   const interval = setInterval(...)
   ref.current = interval
   return () => clearInterval(interval)
}, [])

Then, you can clear the interval whenever using :

clearInterval(ref.current)

Remember to use useCallback hook on your axios call function to make sure react doesn't unnecessarily renders.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745141309a4613444.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信