There's a way to implement a token interceptor? #931
-
Hi, i need to create a token interceptor, i look in documentation and don't find a way to provide this. I need to add a bearer token in all requests except login, someone knows if is possible to make this in react-query? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
This has really nothing to do with What you want to do is pass token to If you use
but this get's repetetive pretty quickly. Better solution would be to set up wrapper with global instance of axios and pass token there, this would look more like this:
then you could wrap request (maybe besides /login if you don't want token there):
Having this you can use react-query as usual:
|
Beta Was this translation helpful? Give feedback.
-
As an addition to @jrozbicki 's answer, we could do something very similar. For instance, we could check the status of the error and navigate to the login page in our app. Here is the code:
In this way, we could use them like:
and optionally we could add a hook:
Hope that I've helped! |
Beta Was this translation helpful? Give feedback.
-
Anyhow, i think RQuery having an interceptor would be great. |
Beta Was this translation helpful? Give feedback.
This has really nothing to do with
react-query
itself.What you want to do is pass token to
Authorization
header.If you use
axios
you could do this in every request like:axios.get('http://someapi.com/api/todos', { headers: { Authorization: `Bearer ${token}`})
but this get's repetetive pretty quickly.
Better solution would be to set up wrapper with global instance of axios and pass token there, this would look more like this: