Skip to content

Android mob 787 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 81 additions & 99 deletions android/src/main/java/com/tsauthentication/TsAuthenticationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<Boolean>() {
@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<Boolean>() {
@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<RegistrationResult>() {
@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<RegistrationResult>() {
@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<AuthenticationResult>() {
@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<AuthenticationResult>() {
@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<AuthenticationResult>() {
@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<AuthenticationResult>() {
@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());
}
}
});
);
}
}
}

2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default class App extends React.Component<any, State> {
TSAuthenticationSDKModule.initialize(
appConfiguration.clientId,
appConfiguration.domain,
`${appConfiguration.baseUrl}/cis/v1`
`${appConfiguration.baseUrl}/cis/v1/`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required for android.
Does it iOs work without it? it doesn't seem to be platform specific

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember it being a problem. If you're sure about it maybe it's worth letting the dev team know. It's not expected (or documented I believe).

);
}

Expand Down