diff --git a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java index 3286253..b7f9933 100644 --- a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java +++ b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java @@ -8,7 +8,6 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableNativeMap; import com.facebook.react.module.annotations.ReactModule; @@ -23,9 +22,11 @@ @ReactModule(name = TsAuthenticationModule.NAME) public class TsAuthenticationModule extends ReactContextBaseJavaModule { public static final String NAME = "TsAuthentication"; + ReactApplicationContext reactContext; public TsAuthenticationModule(ReactApplicationContext reactContext) { super(reactContext); + this.reactContext = reactContext; } @Override @@ -36,17 +37,15 @@ public String getName() { @ReactMethod @NonNull public void initialize(String clientId, String domain, String baseUrl, Promise promise) { - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - TSAuthentication.init(getReactApplicationContext(), - baseUrl, - clientId - ); - promise.resolve(true); - } - }); + + if(reactContext.getCurrentActivity() != null) { + TSAuthentication.init( + reactContext, + baseUrl, + clientId + ); + promise.resolve(true); + } } // Registration @@ -56,108 +55,91 @@ public void run() { String username, String displayName, Promise promise) { - - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - TSAuthentication.isPlatformAuthenticatorSupported( - getReactApplicationContext(), - new TSAuthCallback() { - @Override - public void success(Boolean aBoolean) { - continueRegistration(username, displayName, promise); - } - - @Override - public void error(@NonNull AuthenticationError authenticationError) { - promise.reject(new Error("Unsupported platform")); - } - } - ); + if(reactContext.getCurrentActivity() != null) { + TSAuthentication.isPlatformAuthenticatorSupported( + reactContext.getCurrentActivity(), + new TSAuthCallback() { + @Override + public void success(Boolean aBoolean) { + continueRegistration(username, displayName, promise); + } + + @Override + public void error(@NonNull AuthenticationError authenticationError) { + promise.reject(new Error("Unsupported platform")); + } } - }); + ); + } } private void continueRegistration(String username, String displayName, Promise promise) { - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - TSAuthentication.register( - getReactApplicationContext(), - username, - displayName, - new TSAuthCallback() { - @Override - public void success(RegistrationResult registrationResult) { - WritableMap map = new WritableNativeMap(); - map.putString(registrationResult.result(), NAME); - promise.resolve(map); - } - - @Override - public void error(@NonNull AuthenticationError authenticationError) { - promise.reject(NAME, authenticationError.toString()); - } - } - ); + if(reactContext.getCurrentActivity() != null) { + TSAuthentication.register( + reactContext.getCurrentActivity(), + username, + displayName, + new TSAuthCallback() { + @Override + public void success(RegistrationResult registrationResult) { + WritableMap map = new WritableNativeMap(); + map.putString(registrationResult.result(), NAME); + promise.resolve(map); + } + + @Override + public void error(@NonNull AuthenticationError authenticationError) { + promise.reject(NAME, authenticationError.toString()); + } } - }); + ); } // Authentication @ReactMethod @NonNull public void authenticate(String username, Promise promise) { - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - TSAuthentication.authenticate( - getReactApplicationContext(), - username, - new TSAuthCallback() { - @Override - public void success(AuthenticationResult authenticationResult) { - WritableMap map = new WritableNativeMap(); - map.putString(authenticationResult.result(), NAME); - promise.resolve(map); - } - - @Override - public void error(@NonNull AuthenticationError authenticationError) { - promise.reject(NAME, authenticationError.toString()); - } + if(reactContext.getCurrentActivity() != null) { + TSAuthentication.authenticate( + reactContext.getCurrentActivity(), + username, + new TSAuthCallback() { + @Override + public void success(AuthenticationResult authenticationResult) { + WritableMap map = new WritableNativeMap(); + map.putString(authenticationResult.result(), NAME); + promise.resolve(map); } - ); - } - }); + + @Override + public void error(@NonNull AuthenticationError authenticationError) { + promise.reject(NAME, authenticationError.toString()); + } + } + ); + } + } } @ReactMethod @NonNull public void signTransaction(String username, Promise promise) { - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - TSAuthentication.signTransaction( - getReactApplicationContext(), - username, - new TSAuthCallback() { - @Override - public void success(AuthenticationResult authenticationResult) { - WritableMap map = new WritableNativeMap(); - map.putString(authenticationResult.result(), NAME); - promise.resolve(map); - } - - @Override - public void error(@NonNull AuthenticationError authenticationError) { - promise.reject(NAME, authenticationError.toString()); - } - } - ); + if(reactContext.getCurrentActivity() != null) { + TSAuthentication.signTransaction( + reactContext.getCurrentActivity(), + username, + new TSAuthCallback() { + @Override + public void success(AuthenticationResult authenticationResult) { + WritableMap map = new WritableNativeMap(); + map.putString(authenticationResult.result(), NAME); + promise.resolve(map); + } + + @Override + public void error(@NonNull AuthenticationError authenticationError) { + promise.reject(NAME, authenticationError.toString()); + } } - }); + ); + } } } diff --git a/example/src/App.tsx b/example/src/App.tsx index 1560bf3..24c768e 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -188,7 +188,7 @@ export default class App extends React.Component { TSAuthenticationSDKModule.initialize( appConfiguration.clientId, appConfiguration.domain, - `${appConfiguration.baseUrl}/cis/v1` + `${appConfiguration.baseUrl}/cis/v1/` ); }