Skip to content

Commit

Permalink
Merge pull request #404 from okta/createConfig_callback
Browse files Browse the repository at this point in the history
Use native callbacks instead of promise in createConfig
  • Loading branch information
rajdeepnanua-okta authored Jan 16, 2024
2 parents 5ffcd46 + fd592da commit 2120f8c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
Expand Down Expand Up @@ -99,7 +100,8 @@ public void createConfig(String clientId,
String androidChromeTabColor,
ReadableMap timeouts,
Boolean browserMatchAll,
Promise promise
Callback successCallback,
Callback errorCallback
) {

try {
Expand Down Expand Up @@ -128,7 +130,7 @@ public void createConfig(String clientId,
webAuthBuilder.withTabColor(Color.parseColor(androidChromeTabColor));
} catch (IllegalArgumentException e) {
// The color wasn't in the right format.
promise.reject(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e);
errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e);
}
}

Expand All @@ -142,15 +144,15 @@ public void createConfig(String clientId,
configureBuilder(authClientBuilder, userAgentTemplate, requireHardwareBackedKeyStore, connectTimeout, readTimeout);
this.authClient = authClientBuilder.create();

promise.resolve(true);
successCallback.invoke(true);

if (SESSION_CLIENT_WEB.equals(sharedPreferences.getString(PREFS_KEY, SESSION_CLIENT_WEB))) {
sessionClient = this.webClient.getSessionClient();
} else {
sessionClient = this.authClient.getSessionClient();
}
} catch (Exception e) {
promise.reject(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e);
errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e);
}
}

Expand Down
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,47 @@ export const createConfig = async({
if (Platform.OS === 'ios') {
scopes = scopes.join(' ');

return NativeModules.OktaSdkBridge.createConfig(
NativeModules.OktaSdkBridge.createConfig(
clientId,
redirectUri,
endSessionRedirectUri,
discoveryUri,
scopes,
userAgentTemplate,
httpConnectionTimeout,
successResponse => {
return successResponse;
},
errorResponse => {
return errorResponse;
}
);
}
} else {

const timeouts = {
httpConnectionTimeout,
httpReadTimeout,
};

return NativeModules.OktaSdkBridge.createConfig(
clientId,
redirectUri,
endSessionRedirectUri,
discoveryUri,
scopes,
userAgentTemplate,
requireHardwareBackedKeyStore,
androidChromeTabColor,
timeouts,
browserMatchAll,
);
const timeouts = {
httpConnectionTimeout,
httpReadTimeout,
};

NativeModules.OktaSdkBridge.createConfig(
clientId,
redirectUri,
endSessionRedirectUri,
discoveryUri,
scopes,
userAgentTemplate,
requireHardwareBackedKeyStore,
androidChromeTabColor,
timeouts,
browserMatchAll,
successResponse => {
return successResponse;
},
errorResponse => {
return errorResponse;
}
);
}
};

export const getAuthClient = () => {
Expand Down
8 changes: 4 additions & 4 deletions ios/OktaSdkBridge/OktaSdkBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class OktaSdkBridge: RCTEventEmitter {
scopes: String,
userAgentTemplate: String,
requestTimeout: Int,
promiseResolver: RCTPromiseResolveBlock,
promiseRejecter: RCTPromiseRejectBlock) {
successCallback: RCTResponseSenderBlock,
errorCallback: RCTResponseSenderBlock) {
do {
let uaVersion = OktaUserAgent.userAgentVersion()
let userAgent = userAgentTemplate.replacingOccurrences(of: "$UPSTREAM_SDK", with: "okta-oidc-ios/\(uaVersion)")
Expand All @@ -106,9 +106,9 @@ class OktaSdkBridge: RCTEventEmitter {
oktaOidc = try OktaOidc(configuration: config)
self.requestTimeout = requestTimeout

promiseResolver(true)
successCallback([true])
} catch let error {
promiseRejecter(OktaReactNativeError.oktaOidcError.errorCode, error.localizedDescription, error)
errorCallback([OktaReactNativeError.oktaOidcError.errorCode, error.localizedDescription, error])
}
}

Expand Down
2 changes: 1 addition & 1 deletion ios/Tests/OktaSdkBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class OktaSdkBridgeTests: XCTestCase {
requestTimeout: 20) { (result) in
XCTAssertNotNil(result)
expectation.fulfill()
} promiseRejecter: { (_, _, _) in
} errorCallback: { (_) in
XCTAssert(false, "createConfig failed.")
}

Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ describe('OktaReactNative', () => {
processedScope,
`okta-react-native/${version} $UPSTREAM_SDK react-native/${reactNativeVersion} ios/1.0.0`,
defaultTimeouts.httpConnectionTimeout,
expect.any(Function), // Native bridge success callback
expect.any(Function) // Native bridge error callback
);
});

Expand All @@ -177,6 +179,8 @@ describe('OktaReactNative', () => {
undefined,
defaultTimeouts,
false,
expect.any(Function), // Native bridge success callback
expect.any(Function) // Native bridge error callback
);
});

Expand All @@ -198,6 +202,8 @@ describe('OktaReactNative', () => {
'#FF00AA',
defaultTimeouts,
false,
expect.any(Function), // Native bridge success callback
expect.any(Function) // Native bridge error callback
);
});

Expand All @@ -219,6 +225,8 @@ describe('OktaReactNative', () => {
undefined,
defaultTimeouts,
true,
expect.any(Function), // Native bridge success callback
expect.any(Function) // Native bridge error callback
);
});

Expand All @@ -240,6 +248,8 @@ describe('OktaReactNative', () => {
undefined,
{ httpConnectionTimeout: 12, httpReadTimeout: 34 },
false,
expect.any(Function), // Native bridge success callback
expect.any(Function) // Native bridge error callback
);
});

Expand Down

0 comments on commit 2120f8c

Please sign in to comment.