Geoff Ferrari
Geoff Ferrari
KKinde
Created by Geoff Ferrari on 7/26/2024 in #💻┃support
How to use React SDK with axios
I have a frontend app using @kinde-oss/kinde-auth-react. When I make requests to my backend API server, I want to use axios (axios-http.com) to make the HTTP calls with my Kinde access token. The most natural way of adding the access token to my axios calls is to configure axios with an interceptor which will attempt to retrieve an access token and, if successful, add it as a header. The problem is that the getToken function is provided by useKindeAuth which is a React Hook. But I create my axios instance outside of any React page (in a plain .tsx file) and simply import it when needed. Since my axios instance is created outside of any React component, I'm not able to use the useKindeAuth hook there and so I'm not able to get a reference to the getToken function. So I can't currently configure Axios with an access token. Has anyone come across this problem? Or does anyone have a different suggestion for how to integrate kinde-auth-react with Axios?
5 replies