Home > Net >  How can I deal with Api Tokens(s) that expire?
How can I deal with Api Tokens(s) that expire?

Time:03-07

i was trying to get users of an api in Reactjs, and to catch users they return me a token that expires in a few seconds, and when that time runs my console.log() returns an undefined, how can I store those users before the time runs out ?

my code:

        const router = useRouter()
        const {code} = router.query

        const [valTop, valTopState] = useState('')

        async function userName(){
        
            const a = await axios.get('http://localhost:3001/', { params: {
                code: code
            } })
            
            valTopState(a.data.name)
        }

        userName()

CodePudding user response:

You are likely using Javascript Web Tokens (JWT). When you authenticate it'll give you a regular token and a refresh token. The refresh token is used to generate a new token when the old ones expire, as you've described. The particulars on how to use the refresh token should be explained in the API you're accessing.

Here are a few articles to try:

https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/ https://medium.com/swlh/authentication-using-jwt-and-refresh-token-part-1-aca5522c14c8

CodePudding user response:

Share your code snippet please.

  • Related