Built-in Middleware
Authentication Middleware
Automatically handles token refresh and authentication for all requests whentokenStorage
is provided:
- Automatic token refresh before expiry
- Anonymous token handling with API key
- Token storage synchronization
- Seamless authentication state transitions
Debug Middleware
Request/response logging for development when debug mode is enabled:- Request URLs, headers, body
- Response status, headers, body
- Token refresh events
- Error details
- Network timing
Timeout Middleware
Request timeout handling when configured:Token Storage Integration
The SDK’s automatic token management requires a token storage implementation. For detailed information about all available token storage options, configuration, and best practices, see our comprehensive Token Management Guide.Quick Setup
The token storage you choose affects how the middleware handles authentication. See Token Management for complete storage options including BrowserTokenStorage, CookieTokenStorage, MemoryTokenStorage, and custom implementations.
Authentication Flow
The authentication middleware handles token management automatically:1
Request Initiated
Client makes API request
2
Token Check
Middleware checks if access token exists and is valid
3
Token Refresh
If token is expired, automatically refreshes using refresh token
4
Storage Update
Updates token storage with new tokens
5
Request Completion
Original request proceeds with valid token
Manual Token Management
WithouttokenStorage
, tokens must be managed manually:
Framework-Specific Storage
For framework-specific token storage implementations and patterns:Next.js Integration
Universal token storage for SSR/SSG applications
React Integration
Browser-based storage with React context patterns
Token Callbacks
React to token changes with callback functions:Cross-References
- Token Management: Token Management Guide for complete token handling documentation
- Authentication API: Authentication Reference for endpoint details
- Storefront Auth Guide: Authentication Best Practices for business logic
The middleware system is internal to the SDK and automatically configured based on your options. You cannot add custom middleware - the SDK handles authentication, debugging, and timeout management internally.