Skip to main content

Documentation: Authenticated Axios for API Requests

What it does

Creates an Axios instance that automatically adds your authentication token to every request. (see https://axios-http.com/docs/intro)

How to use it

import useAuthenticatedAxiosInstance from '@hooks/useAuthenticatedAxiosInstance';

function MyComponent() {
// Get the authenticated instance
const axios = useAuthenticatedAxiosInstance();

// Use it like regular axios - authentication happens automatically
const fetchData = async () => {
try {
const response = await axios.get('/endpoint');
return response.data;
} catch (error) {
// handle request errors
}
};
}

Common authentication errors

ErrorWhat it meansPossible actions
401 UnauthorizedUser isn't logged in or token expiredredirect to login page
403 ForbiddenUser doesn't have permissionshow no permission message and redirect back

Features

  • Works just like regular Axios
  • Automatically adds "Authorization: Bearer [token]" header
  • Uses the backend URL as default
  • Updates when your authentication changes