Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Auth0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { IAuth0Client } from './core/interfaces/IAuth0Client';
import { Auth0ClientFactory } from './factory/Auth0ClientFactory';
import type { Auth0Options } from './types';

/**
* The main Auth0 client class.
*
* This class acts as a facade, creating and delegating to a platform-specific
* client instance (Native or Web) under the hood.
*
* @example
* ```
* import Auth0 from 'react-native-auth0';
*
* const auth0 = new Auth0({
* domain: 'YOUR_AUTH0_DOMAIN',
* clientId: 'YOUR_AUTH0_CLIENT_ID'
* });
* ```
*/
export class Auth0 {
private client: IAuth0Client;

/**
* Creates an instance of the Auth0 client.
* @param options Configuration options for the client.
*/
constructor(options: Auth0Options) {
// The factory detects the platform and returns the appropriate client implementation.
// The rest of this class is completely unaware of whether it's running on native or web.
this.client = Auth0ClientFactory.createClient(options);
}

/**
* Provides access to the web-based authentication methods.
* @see IWebAuthProvider
*/
get webAuth() {
return this.client.webAuth;
}

/**
* Provides access to the credentials management methods.
* @see ICredentialsManager
*/
get credentialsManager() {
return this.client.credentialsManager;
}

/**
* Provides access to direct authentication methods (e.g., password-realm).
* @see IAuthenticationProvider
*/
get auth() {
return this.client.auth;
}

/**
* Provides access to the Management API (e.g., for user patching).
*/
users(token: string) {
return this.client.users(token);
}
}
10 changes: 5 additions & 5 deletions src/core/services/AuthenticationOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../models';
import { validateParameters } from '../utils/validation';
import { HttpClient } from './HttpClient';
import { deepCamelCase } from '../utils';
import { deepCamelCase, finalizeScope } from '../utils';

// Represents the raw user profile returned by an API (snake_case)
type RawUser = { [key: string]: any };
Expand Down Expand Up @@ -107,7 +107,7 @@ export class AuthenticationOrchestrator implements IAuthenticationProvider {
subject_token_type: payload.subjectTokenType,
user_profile: payload.userProfile,
audience: payload.audience,
scope: payload.scope,
scope: finalizeScope(payload.scope),
};
const { json, response } =
await this.client.post<NativeCredentialsResponse>(
Expand All @@ -131,7 +131,7 @@ export class AuthenticationOrchestrator implements IAuthenticationProvider {
password: payload.password,
realm: payload.realm,
audience: payload.audience,
scope: payload.scope,
scope: finalizeScope(payload.scope),
};
const { json, response } =
await this.client.post<NativeCredentialsResponse>(
Expand Down Expand Up @@ -212,7 +212,7 @@ export class AuthenticationOrchestrator implements IAuthenticationProvider {
otp: payload.code,
realm: 'email',
audience: payload.audience,
scope: payload.scope,
scope: finalizeScope(payload.scope),
};
const { json, response } =
await this.client.post<NativeCredentialsResponse>(
Expand All @@ -234,7 +234,7 @@ export class AuthenticationOrchestrator implements IAuthenticationProvider {
otp: payload.code,
realm: 'sms',
audience: payload.audience,
scope: payload.scope,
scope: finalizeScope(payload.scope),
};
const { json, response } =
await this.client.post<NativeCredentialsResponse>(
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/Auth0Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ import type {
NativeClearSessionOptions,
} from '../types/platform-specific';
import { Auth0User, AuthError } from '../core/models';
import Auth0 from '../index';
import { Platform } from 'react-native';
import { Auth0 } from '../Auth0';

export const Auth0Provider = ({
children,
...options
}: PropsWithChildren<Auth0Options>) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
const client = useMemo(() => new Auth0(options), []);
const client = useMemo(() => {
const auth0Client = new Auth0(options);
return {
webAuth: auth0Client.webAuth,
credentialsManager: auth0Client.credentialsManager,
auth: auth0Client.auth,
users: (token: string) => auth0Client.users(token),
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const [state, dispatch] = useReducer(reducer, {
user: null,
error: null,
Expand Down
65 changes: 1 addition & 64 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { IAuth0Client } from './core/interfaces/IAuth0Client';
import { Auth0ClientFactory } from './factory/Auth0ClientFactory';
import type { Auth0Options } from './types';
import { Auth0 } from './Auth0';

export {
AuthError,
Expand All @@ -17,65 +15,4 @@ export type {
LocalAuthenticationStrategy,
} from './types/platform-specific';

/**
* The main Auth0 client class.
*
* This class acts as a facade, creating and delegating to a platform-specific
* client instance (Native or Web) under the hood.
*
* @example
* ```
* import Auth0 from 'react-native-auth0';
*
* const auth0 = new Auth0({
* domain: 'YOUR_AUTH0_DOMAIN',
* clientId: 'YOUR_AUTH0_CLIENT_ID'
* });
* ```
*/
class Auth0 {
private client: IAuth0Client;

/**
* Creates an instance of the Auth0 client.
* @param options Configuration options for the client.
*/
constructor(options: Auth0Options) {
// The factory detects the platform and returns the appropriate client implementation.
// The rest of this class is completely unaware of whether it's running on native or web.
this.client = Auth0ClientFactory.createClient(options);
}

/**
* Provides access to the web-based authentication methods.
* @see IWebAuthProvider
*/
get webAuth() {
return this.client.webAuth;
}

/**
* Provides access to the credentials management methods.
* @see ICredentialsManager
*/
get credentialsManager() {
return this.client.credentialsManager;
}

/**
* Provides access to direct authentication methods (e.g., password-realm).
* @see IAuthenticationProvider
*/
get auth() {
return this.client.auth;
}

/**
* Provides access to the Management API (e.g., for user patching).
*/
users(token: string) {
return this.client.users(token);
}
}

export default Auth0;