Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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);
}
}
18 changes: 17 additions & 1 deletion src/core/models/Auth0User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { jwtDecode } from 'jwt-decode';
import type { User } from '../../types';
import { snakeToCamel } from '../utils';

/**
* A private helper that converts a single snake_case string to camelCase.
* Inlined to avoid module loading issues at runtime.
* e.g., 'given_name' -> 'givenName'
*/
function snakeToCamel(str: string): string {
var parts = str.split('_').filter((part) => part.length > 0);
if (parts.length === 0) return '';

return parts.reduce(function (p, c, index) {
if (index === 0) {
return c.charAt(0).toLowerCase() + c.slice(1);
}
return p + c.charAt(0).toUpperCase() + c.slice(1);
}, '');
}

/**
* A Set containing all OIDC protocol claims that are part of a standard ID token
Expand Down
6 changes: 3 additions & 3 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 @@ -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
2 changes: 1 addition & 1 deletion src/hooks/Auth0Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ 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,
Expand Down
66 changes: 2 additions & 64 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { IAuth0Client } from './core/interfaces/IAuth0Client';
import { Auth0ClientFactory } from './factory/Auth0ClientFactory';
import type { Auth0Options } from './types';
import { Auth0 } from './Auth0';

export {
AuthError,
CredentialsManagerError,
WebAuthError,
} from './core/models';
export { TimeoutError } from './core/utils/fetchWithTimeout';
export { snakeToCamel, deepCamelCase } from './core/utils/conversion';
export { Auth0Provider } from './hooks/Auth0Provider';
export { useAuth0 } from './hooks/useAuth0';
export * from './types';
Expand All @@ -17,65 +16,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;