Skip to content
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

Android mob 787 #1

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 @@ -9,14 +9,14 @@

const enum AppScreen {
Home = 'Home',
AuthenticatedUser = 'AuthenticatedUser'

Check failure on line 12 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

export type State = {
errorMessage: string,

Check failure on line 16 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `,` with `;`
currentScreen: string,

Check failure on line 17 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `,` with `;`
username: string,

Check failure on line 18 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `,` with `;`
isNewlyRegistered: boolean

Check failure on line 19 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
};

export type ExampleAppConfiguration = {
Expand All @@ -24,9 +24,9 @@
domain: string;
baseUrl: string;
secret: string;
}

Check failure on line 27 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

export default class App extends React.Component<any, State> {

Check failure on line 29 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

private mockServer!: MockServer;

Expand All @@ -35,18 +35,18 @@
this.state = {
errorMessage: '',
currentScreen: AppScreen.Home,
username: "",

Check failure on line 38 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `""` with `''`
isNewlyRegistered: false

Check failure on line 39 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
};
}

componentDidMount(): void {
this.onAppReady().catch(e => void e);

Check failure on line 44 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `e` with `(e)`

Check warning on line 44 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'
}

render() {
return (
<SafeAreaView style={{ flex: 1 }}>

Check warning on line 49 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { flex: 1 }
{
this.state.currentScreen === AppScreen.Home ? (
<HomeScreen onStartAuthentication={this.onStartAuthentication} errorMessage={this.state.errorMessage}
Expand Down Expand Up @@ -188,7 +188,7 @@
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
Loading