2323@ ReactModule (name = TsAuthenticationModule .NAME )
2424public class TsAuthenticationModule extends ReactContextBaseJavaModule {
2525 public static final String NAME = "TsAuthentication" ;
26+ ReactApplicationContext reactContext ;
2627
2728 public TsAuthenticationModule (ReactApplicationContext reactContext ) {
2829 super (reactContext );
30+ this .reactContext = reactContext ;
2931 }
3032
3133 @ Override
@@ -36,17 +38,15 @@ public String getName() {
3638
3739 @ ReactMethod
3840 @ NonNull public void initialize (String clientId , String domain , String baseUrl , Promise promise ) {
39- UiThreadUtil .runOnUiThread (
40- new Runnable () {
41- @ Override
42- public void run () {
43- TSAuthentication .init (getReactApplicationContext (),
44- baseUrl ,
45- clientId
46- );
47- promise .resolve (true );
48- }
49- });
41+
42+ if (reactContext .getCurrentActivity () != null ) {
43+ TSAuthentication .init (
44+ reactContext ,
45+ baseUrl , // TODO - "https://webauthn.identity.security/v1/"
46+ clientId
47+ );
48+ promise .resolve (true );
49+ }
5050 }
5151
5252 // Registration
@@ -56,108 +56,91 @@ public void run() {
5656 String username ,
5757 String displayName ,
5858 Promise promise ) {
59-
60- UiThreadUtil .runOnUiThread (
61- new Runnable () {
62- @ Override
63- public void run () {
64- TSAuthentication .isPlatformAuthenticatorSupported (
65- getReactApplicationContext (),
66- new TSAuthCallback <Boolean >() {
67- @ Override
68- public void success (Boolean aBoolean ) {
69- continueRegistration (username , displayName , promise );
70- }
71-
72- @ Override
73- public void error (@ NonNull AuthenticationError authenticationError ) {
74- promise .reject (new Error ("Unsupported platform" ));
75- }
76- }
77- );
59+ if (reactContext .getCurrentActivity () != null ) {
60+ TSAuthentication .isPlatformAuthenticatorSupported (
61+ reactContext .getCurrentActivity (),
62+ new TSAuthCallback <Boolean >() {
63+ @ Override
64+ public void success (Boolean aBoolean ) {
65+ continueRegistration (username , displayName , promise );
66+ }
67+
68+ @ Override
69+ public void error (@ NonNull AuthenticationError authenticationError ) {
70+ promise .reject (new Error ("Unsupported platform" ));
71+ }
7872 }
79- });
73+ );
74+ }
8075 }
8176 private void continueRegistration (String username , String displayName , Promise promise ) {
82- UiThreadUtil .runOnUiThread (
83- new Runnable () {
84- @ Override
85- public void run () {
86- TSAuthentication .register (
87- getReactApplicationContext (),
88- username ,
89- displayName ,
90- new TSAuthCallback <RegistrationResult >() {
91- @ Override
92- public void success (RegistrationResult registrationResult ) {
93- WritableMap map = new WritableNativeMap ();
94- map .putString (registrationResult .result (), NAME );
95- promise .resolve (map );
96- }
97-
98- @ Override
99- public void error (@ NonNull AuthenticationError authenticationError ) {
100- promise .reject (NAME , authenticationError .toString ());
101- }
102- }
103- );
77+ if (reactContext .getCurrentActivity () != null ) {
78+ TSAuthentication .register (
79+ reactContext .getCurrentActivity (),
80+ username ,
81+ displayName ,
82+ new TSAuthCallback <RegistrationResult >() {
83+ @ Override
84+ public void success (RegistrationResult registrationResult ) {
85+ WritableMap map = new WritableNativeMap ();
86+ map .putString (registrationResult .result (), NAME );
87+ promise .resolve (map );
88+ }
89+
90+ @ Override
91+ public void error (@ NonNull AuthenticationError authenticationError ) {
92+ promise .reject (NAME , authenticationError .toString ());
93+ }
10494 }
105- } );
95+ );
10696 }
10797
10898 // Authentication
10999 @ ReactMethod
110100 @ NonNull public void authenticate (String username , Promise promise ) {
111- UiThreadUtil .runOnUiThread (
112- new Runnable () {
113- @ Override
114- public void run () {
115- TSAuthentication .authenticate (
116- getReactApplicationContext (),
117- username ,
118- new TSAuthCallback <AuthenticationResult >() {
119- @ Override
120- public void success (AuthenticationResult authenticationResult ) {
121- WritableMap map = new WritableNativeMap ();
122- map .putString (authenticationResult .result (), NAME );
123- promise .resolve (map );
124- }
125-
126- @ Override
127- public void error (@ NonNull AuthenticationError authenticationError ) {
128- promise .reject (NAME , authenticationError .toString ());
129- }
101+ if (reactContext .getCurrentActivity () != null ) {
102+ TSAuthentication .authenticate (
103+ reactContext .getCurrentActivity (),
104+ username ,
105+ new TSAuthCallback <AuthenticationResult >() {
106+ @ Override
107+ public void success (AuthenticationResult authenticationResult ) {
108+ WritableMap map = new WritableNativeMap ();
109+ map .putString (authenticationResult .result (), NAME );
110+ promise .resolve (map );
130111 }
131- );
132- }
133- });
112+
113+ @ Override
114+ public void error (@ NonNull AuthenticationError authenticationError ) {
115+ promise .reject (NAME , authenticationError .toString ());
116+ }
117+ }
118+ );
119+ }
120+ }
134121 }
135122
136123 @ ReactMethod
137124 @ NonNull public void signTransaction (String username , Promise promise ) {
138- UiThreadUtil .runOnUiThread (
139- new Runnable () {
140- @ Override
141- public void run () {
142- TSAuthentication .signTransaction (
143- getReactApplicationContext (),
144- username ,
145- new TSAuthCallback <AuthenticationResult >() {
146- @ Override
147- public void success (AuthenticationResult authenticationResult ) {
148- WritableMap map = new WritableNativeMap ();
149- map .putString (authenticationResult .result (), NAME );
150- promise .resolve (map );
151- }
152-
153- @ Override
154- public void error (@ NonNull AuthenticationError authenticationError ) {
155- promise .reject (NAME , authenticationError .toString ());
156- }
157- }
158- );
125+ if (reactContext .getCurrentActivity () != null ) {
126+ TSAuthentication .signTransaction (
127+ reactContext .getCurrentActivity (),
128+ username ,
129+ new TSAuthCallback <AuthenticationResult >() {
130+ @ Override
131+ public void success (AuthenticationResult authenticationResult ) {
132+ WritableMap map = new WritableNativeMap ();
133+ map .putString (authenticationResult .result (), NAME );
134+ promise .resolve (map );
135+ }
136+
137+ @ Override
138+ public void error (@ NonNull AuthenticationError authenticationError ) {
139+ promise .reject (NAME , authenticationError .toString ());
140+ }
159141 }
160- });
142+ );
143+ }
161144 }
162145}
163146
0 commit comments