Skip to content

feat: Add support for custom headers #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2025

Conversation

subhankarmaiti
Copy link
Contributor

🛠️ Changes

This PR adds support for custom headers in the React Native Auth0 SDK, allowing developers to include custom headers in API requests for both global and per-request scenarios.

Key changes include:

  • Added a new headers property to the Auth0Options interface to support global headers across all requests
  • Modified the Auth0 constructor to store and pass headers to the Auth instance
  • Updated the Auth class to accept headers at initialization and in various methods
  • Added support for per-request custom headers that can override global headers
  • Added headers property to the Auth0Provider component for React hooks integration
  • Methods including passwordRealm, userInfo, refreshToken and others now accept custom headers parameter

This change provides developers with greater flexibility when working with APIs that may require custom headers for security, tracking, language preferences, or other requirements.

Usage summary:

// Global headers set during initialization
const auth0 = new Auth0({
  domain: 'YOUR_AUTH0_DOMAIN',
  clientId: 'YOUR_AUTH0_CLIENT_ID',
  headers: {
    'X-API-Version': '2.0',
    'X-App-Name': 'MyReactNativeApp'
  }
});

// Per-request headers for specific methods
auth0.auth.userInfo({
  token: 'user-access-token',
  headers: {
    'X-Custom-Context': 'user-profile',
    'Accept-Language': 'en-US'
  }
});

// With Auth0Provider
<Auth0Provider
  domain={'YOUR_AUTH0_DOMAIN'}
  clientId={'YOUR_CLIENT_ID'}
  headers={{
    'X-Custom-Header': 'global-value',
    'X-App-Version': '1.2.3'
  }}
>
  <App />
</Auth0Provider>

🔗 References

🧪 Testing

This change has been thoroughly tested through:

  • Added unit tests for global headers in Auth0 initialization
  • Added unit tests for per-request custom headers in Auth methods
  • Added tests to verify headers override behavior (per-request headers take precedence over global)
  • Added tests for Auth0Provider component to ensure it passes headers correctly
  • Manual testing with an Auth0 tenant using custom headers for tracking and localization

To test this change:

  1. Initialize Auth0 or Auth0Provider with custom headers
  2. Make API calls with and without per-request headers
  3. Verify in network logs that headers are being correctly sent with requests

✅ Checklist

  • I have read the Auth0 general contribution guidelines
  • All existing and new tests complete without errors
  • All active GitHub checks have passed
  • This change adds unit test coverage
  • This change has been tested on the latest version of the platform/language

@subhankarmaiti subhankarmaiti requested a review from a team as a code owner April 29, 2025 09:59
@subhankarmaiti subhankarmaiti changed the title feat: add support for custom headers feat: Add support for custom headers Apr 29, 2025
@subhankarmaiti subhankarmaiti merged commit b92ece3 into master Apr 30, 2025
4 checks passed
@subhankarmaiti subhankarmaiti deleted the SDK-5877-allow-custom-header branch April 30, 2025 05:15
@subhankarmaiti subhankarmaiti mentioned this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants