Skip to content

Commit b92ece3

Browse files
feat: Add support for custom headers (#1155)
1 parent 7de5e9c commit b92ece3

File tree

11 files changed

+454
-69
lines changed

11 files changed

+454
-69
lines changed

EXAMPLES.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
- [Login with Passwordless](#login-with-passwordless)
1010
- [Create user in database connection](#create-user-in-database-connection)
1111
- [Using HTTPS callback URLs](#using-https-callback-urls)
12+
- [Using Custom Headers](#using-custom-headers)
13+
- [Set global headers during initialization](#set-global-headers-during-initialization)
14+
- [Using custom headers with Auth0Provider component](#using-custom-headers-with-auth0provider-component)
15+
- [Set request-specific headers](#set-request-specific-headers)
1216
- [Management API (Users)](#management-api-users)
1317
- [Patch user with user_metadata](#patch-user-with-user_metadata)
1418
- [Get full user profile](#get-full-user-profile)
@@ -171,6 +175,67 @@ auth0.webAuth
171175
.catch((error) => console.log(error));
172176
```
173177

178+
### Using Custom Headers
179+
180+
You can set custom headers to be included in all requests to the Auth0 API. This can be useful for implementing custom security requirements, logging, or tracking.
181+
182+
#### Set global headers during initialization
183+
184+
Global headers are included in all requests made by the SDK:
185+
186+
```js
187+
// Set global headers during Auth0 initialization
188+
const auth0 = new Auth0({
189+
domain: 'YOUR_AUTH0_DOMAIN',
190+
clientId: 'YOUR_AUTH0_CLIENT_ID',
191+
headers: {
192+
'Accept-Language': 'fr-CA',
193+
'X-Tracking-Id': 'user-tracking-id-123'
194+
}
195+
});
196+
```
197+
198+
199+
#### Using custom headers with Auth0Provider component
200+
201+
If you're using the hooks-based approach with Auth0Provider, you can provide headers during initialization:
202+
203+
```jsx
204+
import { Auth0Provider } from 'react-native-auth0';
205+
206+
// In your app component
207+
<Auth0Provider
208+
domain={'YOUR_AUTH0_DOMAIN'}
209+
clientId={'YOUR_CLIENT_ID'}
210+
headers={{
211+
'Accept-Language': 'fr-CA',
212+
'X-App-Version': '1.2.3'
213+
}}
214+
>
215+
<App />
216+
</Auth0Provider>
217+
```
218+
219+
220+
#### Set request-specific headers
221+
222+
You can also provide headers for specific API calls, which will override global headers with the same name:
223+
224+
```js
225+
// For specific authentication requests
226+
auth0.auth.passwordRealm({
227+
username: '[email protected]',
228+
password: 'password',
229+
realm: 'myconnection',
230+
headers: {
231+
'X-Custom-Header': 'request-specific-value',
232+
'X-Request-ID': 'unique-request-id-456'
233+
}
234+
})
235+
.then(console.log)
236+
.catch(console.error);
237+
```
238+
174239
## Management API (Users)
175240

176241
### Patch user with user_metadata

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ const App = () => {
346346
export default App;
347347
```
348348

349+
You can also pass custom headers that will be included in all API requests:
350+
351+
```js
352+
import { Auth0Provider } from 'react-native-auth0';
353+
354+
const App = () => {
355+
return (
356+
<Auth0Provider
357+
domain="YOUR_AUTH0_DOMAIN"
358+
clientId="YOUR_AUTH0_CLIENT_ID"
359+
headers={{ 'X-Custom-Header': 'custom-value' }}
360+
>
361+
{/* YOUR APP */}
362+
</Auth0Provider>
363+
);
364+
};
365+
366+
export default App;
367+
```
368+
349369
<details>
350370
<summary>Using the `Auth0` class</summary>
351371

@@ -360,6 +380,19 @@ const auth0 = new Auth0({
360380
});
361381
```
362382

383+
You can also pass custom headers that will be included in all API requests:
384+
385+
```js
386+
import Auth0 from 'react-native-auth0';
387+
388+
const auth0 = new Auth0({
389+
domain: 'YOUR_AUTH0_DOMAIN',
390+
clientId: 'YOUR_AUTH0_CLIENT_ID',
391+
headers: {
392+
'X-Custom-Header': 'custom-value',
393+
}
394+
});
395+
```
363396
</details>
364397

365398
Then import the hook into a component where you want to get access to the properties and methods for integrating with Auth0:

src/auth/__tests__/index.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ describe('auth', () => {
5757
it('should fail without domain', () => {
5858
expect(() => new Auth({ clientId })).toThrowErrorMatchingSnapshot();
5959
});
60+
61+
it('should accept custom headers', () => {
62+
const headers = { 'X-Custom-Header': 'custom-value' };
63+
const auth = new Auth({ baseUrl, clientId, headers });
64+
expect(auth.client.globalHeaders).toEqual(headers);
65+
});
6066
});
6167

6268
describe('authorizeUrl', () => {
@@ -1053,4 +1059,52 @@ describe('auth', () => {
10531059
).resolves.toMatchSnapshot();
10541060
});
10551061
});
1062+
1063+
describe('method-specific custom headers', () => {
1064+
it('should accept and use custom headers in passwordRealm that don\'t conflict with defaults', async () => {
1065+
fetchMock.postOnce('https://samples.auth0.com/oauth/token', tokens);
1066+
const customHeaders = { 'X-Custom-Header': 'custom-value' };
1067+
1068+
await auth.passwordRealm({
1069+
username: '[email protected]',
1070+
password: 'secret pass',
1071+
realm: 'Username-Password-Authentication',
1072+
headers: customHeaders
1073+
});
1074+
1075+
const [_, fetchOptions] = fetchMock.lastCall();
1076+
expect(fetchOptions.headers.get('X-Custom-Header')).toBe('custom-value');
1077+
});
1078+
1079+
it('should accept and use custom headers in userInfo that don\'t conflict with defaults', async () => {
1080+
const success = {
1081+
status: 200,
1082+
body: { sub: 'auth0|1029837475' },
1083+
headers: { 'Content-Type': 'application/json' },
1084+
};
1085+
fetchMock.getOnce('https://samples.auth0.com/userinfo', success);
1086+
const customHeaders = { 'X-Custom-Header': 'custom-value' };
1087+
1088+
await auth.userInfo({
1089+
token: 'an access token of a user',
1090+
headers: customHeaders
1091+
});
1092+
1093+
const [_, fetchOptions] = fetchMock.lastCall();
1094+
expect(fetchOptions.headers.get('X-Custom-Header')).toBe('custom-value');
1095+
});
1096+
1097+
it('should accept and use custom headers in refreshToken that don\'t conflict with defaults', async () => {
1098+
fetchMock.postOnce('https://samples.auth0.com/oauth/token', tokens);
1099+
const customHeaders = { 'X-Custom-Header': 'custom-value' };
1100+
1101+
await auth.refreshToken({
1102+
refreshToken: 'a refresh token of a user',
1103+
headers: customHeaders
1104+
});
1105+
1106+
const [_, fetchOptions] = fetchMock.lastCall();
1107+
expect(fetchOptions.headers.get('X-Custom-Header')).toBe('custom-value');
1108+
});
1109+
});
10561110
});

0 commit comments

Comments
 (0)