Skip to content

There's a way to implement a token interceptor? #931

Answered by jrozbicki
veronesecoms asked this question in General
Discussion options

You must be logged in to vote

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:

// request.js
import axios from 'axios';
import { getToken } from 'someUtilsFunctions'

// optionaly add base url
const client = axios.create({ baseURL: 'http://someapi.com/api' });

const request = ({ ...options }) => {
    client.defaults.headers.common.Authorizat…

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
6 replies
@itsmelion
Comment options

@inoumen
Comment options

@itsmelion
Comment options

@leslie555
Comment options

@jrozbicki
Comment options

Answer selected by veronesecoms
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants