Skip to content

Commit fb7c44d

Browse files
Fix: Remove require cycle between index.ts and Auth0Provider.tsx (#1367)
1 parent f9523bb commit fb7c44d

File tree

6 files changed

+113
-103
lines changed

6 files changed

+113
-103
lines changed

src/Auth0.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import type { IAuth0Client } from './core/interfaces/IAuth0Client';
2+
import { Auth0ClientFactory } from './factory/Auth0ClientFactory';
3+
import type { Auth0Options, DPoPHeadersParams } from './types';
4+
5+
/**
6+
* The main Auth0 client class.
7+
*
8+
* This class acts as a facade, creating and delegating to a platform-specific
9+
* client instance (Native or Web) under the hood.
10+
*
11+
* @example
12+
* ```
13+
* import Auth0 from 'react-native-auth0';
14+
*
15+
* const auth0 = new Auth0({
16+
* domain: 'YOUR_AUTH0_DOMAIN',
17+
* clientId: 'YOUR_AUTH0_CLIENT_ID'
18+
* });
19+
* ```
20+
*/
21+
class Auth0 {
22+
private client: IAuth0Client;
23+
24+
/**
25+
* Creates an instance of the Auth0 client.
26+
* @param options Configuration options for the client.
27+
*/
28+
constructor(options: Auth0Options) {
29+
// The factory detects the platform and returns the appropriate client implementation.
30+
// The rest of this class is completely unaware of whether it's running on native or web.
31+
this.client = Auth0ClientFactory.createClient(options);
32+
}
33+
34+
/**
35+
* Provides access to the web-based authentication methods.
36+
* @see IWebAuthProvider
37+
*/
38+
get webAuth() {
39+
return this.client.webAuth;
40+
}
41+
42+
/**
43+
* Provides access to the credentials management methods.
44+
* @see ICredentialsManager
45+
*/
46+
get credentialsManager() {
47+
return this.client.credentialsManager;
48+
}
49+
50+
/**
51+
* Provides access to direct authentication methods (e.g., password-realm).
52+
* @see IAuthenticationProvider
53+
*/
54+
get auth() {
55+
return this.client.auth;
56+
}
57+
58+
/**
59+
* Provides access to the Management API (e.g., for user patching).
60+
*/
61+
users(token: string) {
62+
return this.client.users(token);
63+
}
64+
65+
/**
66+
* Generates DPoP headers for making authenticated requests to custom APIs.
67+
* This method creates the necessary HTTP headers (Authorization and DPoP) to
68+
* securely bind the access token to a specific API request.
69+
*
70+
* @param params Parameters including the URL, HTTP method, access token, and token type.
71+
* @returns A promise that resolves to an object containing the required headers.
72+
*
73+
* @example
74+
* ```typescript
75+
* const credentials = await auth0.credentialsManager.getCredentials();
76+
*
77+
* if (credentials.tokenType === 'DPoP') {
78+
* const headers = await auth0.getDPoPHeaders({
79+
* url: 'https://api.example.com/data',
80+
* method: 'GET',
81+
* accessToken: credentials.accessToken,
82+
* tokenType: credentials.tokenType
83+
* });
84+
*
85+
* const response = await fetch('https://api.example.com/data', { headers });
86+
* }
87+
* ```
88+
*/
89+
getDPoPHeaders(params: DPoPHeadersParams) {
90+
return this.client.getDPoPHeaders(params);
91+
}
92+
}
93+
94+
export default Auth0;

src/exports/classes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export {
33
CredentialsManagerError,
44
WebAuthError,
55
} from '../core/models';
6-
export { default as Auth0, TimeoutError } from '../index';
6+
export { default as Auth0 } from '../Auth0';
7+
export { TimeoutError } from '../core/utils/fetchWithTimeout';

src/hooks/Auth0Provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
NativeClearSessionOptions,
3131
} from '../types/platform-specific';
3232
import { Auth0User, AuthError } from '../core/models';
33-
import Auth0 from '../index';
33+
import Auth0 from '../Auth0';
3434
import { Platform } from 'react-native';
3535

3636
export const Auth0Provider = ({

src/hooks/__tests__/Auth0Provider.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@testing-library/react';
99
import '@testing-library/jest-dom';
1010
import { Auth0Provider, useAuth0 } from '..';
11-
import Auth0 from '../../index';
11+
import Auth0 from '../../Auth0';
1212

1313
// Mock TurboModuleRegistry first
1414
jest.mock('react-native/Libraries/TurboModule/TurboModuleRegistry', () => ({
@@ -63,7 +63,7 @@ jest.mock('react-native', () => ({
6363
}));
6464

6565
// 1. Mock the top-level Auth0 facade
66-
jest.mock('../../index');
66+
jest.mock('../../Auth0');
6767
const MockAuth0 = Auth0 as jest.MockedClass<typeof Auth0>;
6868

6969
// Mock the Auth0User model's factory method

src/index.ts

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import type { IAuth0Client } from './core/interfaces/IAuth0Client';
2-
import { Auth0ClientFactory } from './factory/Auth0ClientFactory';
3-
import type { Auth0Options, DPoPHeadersParams } from './types';
4-
51
export {
62
AuthError,
73
CredentialsManagerError,
@@ -18,93 +14,5 @@ export type {
1814
LocalAuthenticationStrategy,
1915
} from './types/platform-specific';
2016

21-
/**
22-
* The main Auth0 client class.
23-
*
24-
* This class acts as a facade, creating and delegating to a platform-specific
25-
* client instance (Native or Web) under the hood.
26-
*
27-
* @example
28-
* ```
29-
* import Auth0 from 'react-native-auth0';
30-
*
31-
* const auth0 = new Auth0({
32-
* domain: 'YOUR_AUTH0_DOMAIN',
33-
* clientId: 'YOUR_AUTH0_CLIENT_ID'
34-
* });
35-
* ```
36-
*/
37-
class Auth0 {
38-
private client: IAuth0Client;
39-
40-
/**
41-
* Creates an instance of the Auth0 client.
42-
* @param options Configuration options for the client.
43-
*/
44-
constructor(options: Auth0Options) {
45-
// The factory detects the platform and returns the appropriate client implementation.
46-
// The rest of this class is completely unaware of whether it's running on native or web.
47-
this.client = Auth0ClientFactory.createClient(options);
48-
}
49-
50-
/**
51-
* Provides access to the web-based authentication methods.
52-
* @see IWebAuthProvider
53-
*/
54-
get webAuth() {
55-
return this.client.webAuth;
56-
}
57-
58-
/**
59-
* Provides access to the credentials management methods.
60-
* @see ICredentialsManager
61-
*/
62-
get credentialsManager() {
63-
return this.client.credentialsManager;
64-
}
65-
66-
/**
67-
* Provides access to direct authentication methods (e.g., password-realm).
68-
* @see IAuthenticationProvider
69-
*/
70-
get auth() {
71-
return this.client.auth;
72-
}
73-
74-
/**
75-
* Provides access to the Management API (e.g., for user patching).
76-
*/
77-
users(token: string) {
78-
return this.client.users(token);
79-
}
80-
81-
/**
82-
* Generates DPoP headers for making authenticated requests to custom APIs.
83-
* This method creates the necessary HTTP headers (Authorization and DPoP) to
84-
* securely bind the access token to a specific API request.
85-
*
86-
* @param params Parameters including the URL, HTTP method, access token, and token type.
87-
* @returns A promise that resolves to an object containing the required headers.
88-
*
89-
* @example
90-
* ```typescript
91-
* const credentials = await auth0.credentialsManager.getCredentials();
92-
*
93-
* if (credentials.tokenType === 'DPoP') {
94-
* const headers = await auth0.getDPoPHeaders({
95-
* url: 'https://api.example.com/data',
96-
* method: 'GET',
97-
* accessToken: credentials.accessToken,
98-
* tokenType: credentials.tokenType
99-
* });
100-
*
101-
* const response = await fetch('https://api.example.com/data', { headers });
102-
* }
103-
* ```
104-
*/
105-
getDPoPHeaders(params: DPoPHeadersParams) {
106-
return this.client.getDPoPHeaders(params);
107-
}
108-
}
109-
110-
export default Auth0;
17+
// Re-export Auth0 as default
18+
export { default } from './Auth0';

src/plugin/withAuth0.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export const addAndroidAuth0Manifest = (
3838

3939
const intentFilterContent = [
4040
{
41-
...(hasAppLinks && { $: { 'android:autoVerify': 'true' as AndroidConfig.Manifest.StringBoolean } }),
41+
...(hasAppLinks && {
42+
$: {
43+
'android:autoVerify': 'true' as AndroidConfig.Manifest.StringBoolean,
44+
},
45+
}),
4246
action: [{ $: { 'android:name': 'android.intent.action.VIEW' } }],
4347
category: [
4448
{ $: { 'android:name': 'android.intent.category.DEFAULT' } },
@@ -67,14 +71,16 @@ export const addAndroidAuth0Manifest = (
6771
'tools:node': 'replace',
6872
'android:exported': 'true',
6973
},
70-
'intent-filter': intentFilterContent as AndroidConfig.Manifest.ManifestIntentFilter[],
74+
'intent-filter':
75+
intentFilterContent as AndroidConfig.Manifest.ManifestIntentFilter[],
7176
};
7277
mainApplication.activity = mainApplication.activity || [];
7378
mainApplication.activity.push(redirectActivity);
7479
}
75-
80+
7681
redirectActivity['intent-filter'] =
77-
redirectActivity['intent-filter'] || intentFilterContent as AndroidConfig.Manifest.ManifestIntentFilter[];
82+
redirectActivity['intent-filter'] ||
83+
(intentFilterContent as AndroidConfig.Manifest.ManifestIntentFilter[]);
7884
const intentFilter = redirectActivity['intent-filter'][0] || {};
7985
if (!intentFilter) {
8086
throw new Error('Failed to create intent filter');
@@ -84,7 +90,8 @@ export const addAndroidAuth0Manifest = (
8490
// Add android:autoVerify="true" for App Links
8591
if (hasAppLinks) {
8692
intentFilter.$ = intentFilter.$ || {};
87-
intentFilter.$['android:autoVerify'] = 'true' as AndroidConfig.Manifest.StringBoolean;
93+
intentFilter.$['android:autoVerify'] =
94+
'true' as AndroidConfig.Manifest.StringBoolean;
8895
}
8996

9097
// Add data elements for each auth0Config

0 commit comments

Comments
 (0)