Skip to content

Commit

Permalink
chore: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
desusai7 committed Jul 31, 2024
1 parent 92ba9af commit ccd4fc2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions android/src/main/java/com/auth0/react/A0Auth0Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public A0Auth0Module(ReactApplicationContext reactContext) {
}

@ReactMethod
public void initializeAuth0(String clientId, String domain) {
public void initializeAuth0WithConfiguration(String clientId, String domain) {
this.auth0 = new Auth0(clientId, domain);
AuthenticationAPIClient authenticationAPIClient = new AuthenticationAPIClient(auth0);
this.secureCredentialsManager = new SecureCredentialsManager(
Expand All @@ -55,7 +55,7 @@ public void initializeAuth0(String clientId, String domain) {
}

@ReactMethod
public void hasValidAuth0Instance(String clientId, String domain, Promise promise) {
public void hasValidAuth0InstanceWithConfiguration(String clientId, String domain, Promise promise) {
if(this.auth0 == null) {
promise.resolve(false);
return;
Expand Down
4 changes: 2 additions & 2 deletions ios/A0Auth0.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ - (dispatch_queue_t)methodQueue

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(hasValidAuth0Instance:(NSString *)clientId domain:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve
RCT_EXPORT_METHOD(hasValidAuth0InstanceWithConfiguration:(NSString *)clientId domain:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
BOOL valid = [self checkHasValidNativeBridgeInstance:clientId domain:domain];
resolve(@(valid));
}


RCT_EXPORT_METHOD(initializeAuth0:(NSString *)clientId domain:(NSString *)domain) {
RCT_EXPORT_METHOD(initializeAuth0WithConfiguration:(NSString *)clientId domain:(NSString *)domain) {
[self tryAndInitializeNativeBridge:clientId domain:domain];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('credentials manager tests', () => {
'abc123'
);

credentialsManager.Auth0Module.hasValidAuth0Instance = () =>
credentialsManager.Auth0Module.hasValidAuth0InstanceWithConfiguration = () =>
Promise.resolve(true);
credentialsManager.Auth0Module.saveCredentials = () => {};
credentialsManager.Auth0Module.getCredentials = () => {};
Expand Down
12 changes: 6 additions & 6 deletions src/credentials-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CredentialsManagerError from './credentialsManagerError';
import LocalAuthenticationStrategy from './localAuthenticationStrategy';
import { Credentials } from '../types';
import { Auth0Module } from 'src/internal-types';
import { _ensureNativeModuleIsInitialized } from '../utils/nativeHelper';
import { _ensureNativeModuleIsInitializedWithConfiguration } from '../utils/nativeHelper';

class CredentialsManager {
private domain;
Expand Down Expand Up @@ -35,7 +35,7 @@ class CredentialsManager {
}
});
try {
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain
Expand Down Expand Up @@ -66,7 +66,7 @@ class CredentialsManager {
forceRefresh: boolean = false
): Promise<Credentials> {
try {
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain
Expand Down Expand Up @@ -103,7 +103,7 @@ class CredentialsManager {
strategy = LocalAuthenticationStrategy.deviceOwnerWithBiometrics
): Promise<void> {
try {
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain
Expand Down Expand Up @@ -134,7 +134,7 @@ class CredentialsManager {
* @returns `true` if a valid set of credentials are available, or `false` if there are no credentials to return.
*/
async hasValidCredentials(minTtl = 0): Promise<boolean> {
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain
Expand All @@ -146,7 +146,7 @@ class CredentialsManager {
* Delete the stored credentials
*/
async clearCredentials(): Promise<void> {
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain
Expand Down
10 changes: 8 additions & 2 deletions src/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ export type Auth0Module = {
| ((title?: string, description?: string) => Promise<void>);
hasValidCredentials: (minTtl?: number) => Promise<boolean>;
clearCredentials: () => Promise<void>;
hasValidAuth0Instance: (clientId: String, domain: String) => Promise<boolean>;
initializeAuth0: (clientId: string, domain: string) => Promise<void>;
hasValidAuth0InstanceWithConfiguration: (
clientId: String,
domain: String
) => Promise<boolean>;
initializeAuth0WithConfiguration: (
clientId: string,
domain: string
) => Promise<void>;
};

export type AgentParameters = {
Expand Down
9 changes: 6 additions & 3 deletions src/utils/nativeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Auth0Module } from 'src/internal-types';

//private
export async function _ensureNativeModuleIsInitialized(
export async function _ensureNativeModuleIsInitializedWithConfiguration(
nativeModule: Auth0Module,
clientId: string,
domain: string
) {
const hasValid = await nativeModule.hasValidAuth0Instance(clientId, domain);
const hasValid = await nativeModule.hasValidAuth0InstanceWithConfiguration(
clientId,
domain
);
if (!hasValid) {
await nativeModule.initializeAuth0(clientId, domain);
await nativeModule.initializeAuth0WithConfiguration(clientId, domain);
}
}
6 changes: 3 additions & 3 deletions src/webauth/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EmitterSubscription,
} from 'react-native';
import { Credentials } from 'src/types';
import { _ensureNativeModuleIsInitialized } from '../utils/nativeHelper';
import { _ensureNativeModuleIsInitializedWithConfiguration } from '../utils/nativeHelper';
import {
AgentLoginOptions,
AgentLogoutOptions,
Expand Down Expand Up @@ -42,7 +42,7 @@ class Agent {
});
}
try {
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
A0Auth0,
parameters.clientId,
parameters.domain
Expand Down Expand Up @@ -96,7 +96,7 @@ class Agent {
);
let redirectUri =
options.returnToUrl ?? this.callbackUri(parameters.domain, scheme);
await _ensureNativeModuleIsInitialized(
await _ensureNativeModuleIsInitializedWithConfiguration(
NativeModules.A0Auth0,
parameters.clientId,
parameters.domain
Expand Down

0 comments on commit ccd4fc2

Please sign in to comment.