This project demonstrates handling token-based authentication and automatic token refreshing using Axios and Auth0 in a React + TypeScript application. Built with Vite, the setup ensures fast development cycles and smooth integration of the latest web technologies.
The project provides a practical implementation of token management in a React application, including:
- Token Authentication: Automatically includes the Auth0 access token in API requests.
- Automatic Token Refresh: Seamlessly refreshes the token when it expires, ensuring uninterrupted API communication.
- Efficient Error Handling: Handles authentication errors by retrying requests with a new token.
-
src/
- api/: Contains API-related code.
api.ts: Defines the API endpoints and handles API requests.auth.ts: Manages authentication logic using Auth0.interceptors.ts: Sets up Axios interceptors to handle authentication and token refreshing.refreshToken.ts: Contains the logic to handle token refreshing, ensuring only one refresh request is processed at a time.
- assets/: Holds static assets like images and other resources.
- components/: Reusable React components.
ProtectedRoute.tsx: A component that guards routes requiring authentication.TestApiComponent.tsx: Example component demonstrating an API call with authentication.index.ts: Centralized exports for components.
- pages/: Main application pages.
HomePage.tsx: The main homepage component.App.tsx: Root component of the application.main.tsx: Entry point for the React application.
- vite-env.d.ts: TypeScript definitions for Vite environment variables.
- api/: Contains API-related code.
-
Clone the repository:
git clone https://github.com/ali-noori-dev/token-refresh-example.git cd project-folder -
Install dependencies:
npm install
-
Run the development server:
npm run dev
-
Build the project for production:
npm run build
- Request Interceptor: The
addAuthTokenfunction ininterceptors.tsadds the Auth0 access token to each API request's Authorization header. - Response Interceptor: The
retryRequestfunction handles 401 Unauthorized errors by invoking therefreshTokenlogic and retrying the original request with a new token.
- The
refreshToken.tsfile ensures that only one token refresh request is made at a time. If a token refresh is in progress, subsequent requests wait for the token refresh to complete before proceeding.
Contributions are welcome! Please open an issue or submit a pull request if you find any issues or have suggestions for improvements.