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

Fixed race condition in callback when using React Native Navigation #100

Merged
merged 2 commits into from
Oct 9, 2017
Merged
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
25 changes: 17 additions & 8 deletions android/src/main/java/com/auth0/react/A0Auth0Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.customtabs.CustomTabsIntent;
import android.util.Base64;
Expand All @@ -28,6 +29,7 @@ public class A0Auth0Module extends ReactContextBaseJavaModule implements Lifecyc

private static final String US_ASCII = "US-ASCII";
private static final String SHA_256 = "SHA-256";
private static final int CANCEL_EVENT_DELAY = 100;

private final ReactApplicationContext reactContext;
private Callback callback;
Expand Down Expand Up @@ -59,6 +61,7 @@ public void showUrl(String url, boolean closeOnLoad, Callback callback) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
customTabsIntent.launchUrl(activity, Uri.parse(url));
} else {
final Intent intent = new Intent(Intent.ACTION_VIEW);
Expand Down Expand Up @@ -125,13 +128,19 @@ String generateCodeChallenge(@NonNull String codeVerifier) {

@Override
public void onHostResume() {
if (this.callback != null) {
final WritableMap error = Arguments.createMap();
error.putString("error", "a0.session.user_cancelled");
error.putString("error_description", "User cancelled the Auth");
this.callback.invoke(error);
this.callback = null;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Callback cb = A0Auth0Module.this.callback;
if (cb != null) {
final WritableMap error = Arguments.createMap();
error.putString("error", "a0.session.user_cancelled");
error.putString("error_description", "User cancelled the Auth");
cb.invoke(error);
A0Auth0Module.this.callback = null;
}
}
}, CANCEL_EVENT_DELAY);
}

@Override
Expand All @@ -143,4 +152,4 @@ public void onHostPause() {
public void onHostDestroy() {

}
}
}