Reputation: 1296
I'm relatively new to React. I'm using the axios-auth-refresh tool at https://www.npmjs.com/package/axios-auth-refresh, although the problem isn't specific to that. I want to be able to handle the case when the refresh token is expired. In that case, I want to re-direct to the login page and then after the user has re-authenticated, direct them back to the page they were originally on. Since the axios code is not part of a component, I can't figure out how to get access to the current page so I can save it.
I discovered that I can add custom config items on the axios call, so I can code axios.post(url, {'currentPage' : '/myCurrentUrl'})
. But that requires me to code that config for every axios call. What I want is to be able to call axios (or call some function which calls Axios), which can figure out what url of the page it was called from.
Is there any way to do that?
Upvotes: 0
Views: 89
Reputation: 1978
In your default props for a component in a router you have access to a location
object. You can pass the location to the axios call like this:
axios.post(url, {'currentPage': props.location.pathname})
Upvotes: 1