From a0dd72b171c6a5b4642f3bacff0866253488a55d Mon Sep 17 00:00:00 2001 From: Ran-Stone Date: Mon, 1 Jan 2024 16:56:16 +0200 Subject: [PATCH 1/3] Fix reactContext.getCurrentActivity() --- .../TsAuthenticationModule.java | 179 ++++++++---------- 1 file changed, 81 insertions(+), 98 deletions(-) diff --git a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java index 3286253..3364a56 100644 --- a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java +++ b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java @@ -23,9 +23,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 +38,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, // TODO - "https://webauthn.identity.security/v1/" + clientId + ); + promise.resolve(true); + } } // Registration @@ -56,108 +56,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()); + } } - }); + ); + } } } From f2f0b4a5dd0569015f82a4543f2dbac7e68f3dbb Mon Sep 17 00:00:00 2001 From: Ran-Stone Date: Mon, 1 Jan 2024 16:57:49 +0200 Subject: [PATCH 2/3] cleanup --- .../main/java/com/tsauthentication/TsAuthenticationModule.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java index 3364a56..2e8c315 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; From 09fedbe9b57f68a3c7512dfa73504525fbbfa233 Mon Sep 17 00:00:00 2001 From: Ran-Stone Date: Tue, 2 Jan 2024 12:50:18 +0200 Subject: [PATCH 3/3] fix base url --- .../main/java/com/tsauthentication/TsAuthenticationModule.java | 2 +- example/src/App.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java index 2e8c315..b7f9933 100644 --- a/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java +++ b/android/src/main/java/com/tsauthentication/TsAuthenticationModule.java @@ -41,7 +41,7 @@ public String getName() { if(reactContext.getCurrentActivity() != null) { TSAuthentication.init( reactContext, - baseUrl, // TODO - "https://webauthn.identity.security/v1/" + baseUrl, clientId ); promise.resolve(true); 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/` ); }