T
TanStack10mo ago
national-gold

Best place to attach Auth token to requests?

Where is the best place to attach my Clerk auth token to my axios for requests? Im migrating from router and had a helper function that attached the token previously, but it doesn’t seem to work the same with Start. Thanks!
1 Reply
national-gold
national-goldOP10mo ago
Currently I'm trying to fetch my getToken in my fetchClerk serverFN const fetchClerkAuth = createServerFn({ method: 'GET' }).handler(async () => { const { userId, getToken } = await getAuth(getWebRequest()); setupAuthInterceptor(getToken); return { userId, }; }); Then pass it to my axios setupInterceptor function export const setupAuthInterceptor = ( getToken: () => Promise<string | null>, ) => { const interceptor = axiosInstance.interceptors.request.use( async (config) => { const token = await getToken(); if (token) { config.headers.Authorization = Bearer ${token}; } return config; }, (error) => { return Promise.reject(error); }, ); return () => { axiosInstance.interceptors.request.eject(interceptor); }; }; But it seems to just randomly only sometimes work

Did you find this page helpful?