Overview
DADDO uses Axios as the HTTP client for all API communications. The API layer is configured with request/response interceptors for automatic token injection, error handling, and session management.Axios Instance Configuration
The application creates a custom Axios instance with base configuration:src/Redux/api.js
The API base URL is configured via the
VITE_URL2 environment variable, allowing different URLs for development, staging, and production environments.Environment Configuration
Create a.env file in your project root:
.env
Request Interceptor
The request interceptor automatically attaches authentication tokens to all outgoing requests:src/Redux/api.js
How It Works
- Token Retrieval: Checks both
localStorageandsessionStoragefor authentication token - Header Injection: Adds
Authorization: Bearer <token>header if token exists - Automatic: No need to manually add auth headers in action creators
Response Interceptor
The response interceptor handles authentication errors and automatic logout:src/Redux/api.js
Error Handling Logic
The interceptor only triggers logout if a token exists, preventing unnecessary logout actions for unauthenticated API calls (like login itself).
Authentication Flow
Login Process
Token-Based Request Flow
API Endpoints Used
Authentication Endpoints
| Method | Endpoint | Description |
|---|---|---|
| POST | /user/login | User login |
| POST | /user/register | User registration |
| POST | /user/forgot-password | Request password reset |
| POST | /user/reset-password | Reset password with token |
| PUT | /user/update | Update user profile |
Product Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /products | Get all user products |
| POST | /products | Create new product |
| PUT | /products/:id | Update product |
| DELETE | /products/:id | Delete product |
| GET | /products/categories | Get product categories |
| GET | /catalog/:userId | Get public catalog |
Sales Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /sells | Get user sales |
| POST | /sells | Create new sale |
| GET | /sells/:id | Get sale details |
| PUT | /sells/:id/confirm | Confirm/complete sale |
| DELETE | /sells/:id | Delete sale |
Dashboard Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /dashboard | Get dashboard analytics |
| GET | /dashboard/profit | Get profit by date range |
Making API Calls in Actions
GET Request Example
POST Request Example
PUT Request Example
DELETE Request Example
Error Handling Patterns
Component-Level Error Handling
Common Error Response Formats
CORS Configuration
The Axios instance is configured with
withCredentials: false. If your API requires cookies or session-based auth, set this to true.Request/Response Debugging
Adding Debug Interceptors
API Best Practices
Always Handle Errors
Always Handle Errors
Every API call should have try/catch blocks and dispatch failure actions for proper error state management.
Use Optional Chaining
Use Optional Chaining
Access nested error properties with optional chaining:
error.response?.data?.messageProvide Fallback Messages
Provide Fallback Messages
Always provide fallback error messages:
error.message || 'An error occurred'Return Data from Actions
Return Data from Actions
Return API response data from action creators for component-level handling:
return response.dataValidate Before Sending
Validate Before Sending
Validate data in components before dispatching API actions to reduce unnecessary requests.
Security Considerations
- XSS Protection: Sanitize user input to prevent token theft
- HTTPS Only: Always use HTTPS in production to prevent token interception
- Token Expiration: Implement token refresh mechanisms for long-lived sessions
- Logout on 401: Automatic logout prevents using expired tokens
Next Steps
State Management
Learn how API responses are managed in Redux
Architecture
Understand how API layer fits into overall architecture