Skip to content

Commit 08ce7f5

Browse files
chore: add explicit error for signin cancelation (#1073)
During sign-in with the Cognito hosted web UI, a Chrome Custom tab will pop up. The user may fill in their sign-in information. However, they may also exit out of the tab, in which case the sign-in will be cancelled. The current behavior is to fire an exception on the onError callback when this occurs. That exception looks like this: ``` AuthException{ message=Sign in with web UI failed cause=[...mobile client...] AuthNavigationException{ message=user cancelled } recoverySuggestion=See attached exception for more details } ``` This commit updates the exception that gets returned, to something more specific, that a user could catch explicitly: ``` UserCancelledException{ message=The user cancelled the sign-in attempt, so it did not complete. cause=[...mobile client...] AuthNavigationException { message=user cancelled } recoverySuggestion=To recover: catch this error, and show the sign-in screen again. } ``` With this change, the caller may catch the cancellation exception in a `when` block: ```kotlin Amplify.Auth.signInWithWebUI(this, { /* result .. */ }, { error -> when (error) { is UserCancelledException -> Log.w("TAG", "sign-in cancelled") else -> Log.e("xx", "Login failed.", error) } } ``` This commit also applies similar updates to the sign-out method. Related: aws-amplify/amplify-swift#982
1 parent f9c2681 commit 08ce7f5

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import com.amazonaws.mobile.client.results.UserCodeDeliveryDetails;
8484
import com.amazonaws.mobile.config.AWSConfiguration;
8585
import com.amazonaws.mobileconnectors.cognitoauth.AuthClient;
86+
import com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException;
8687
import com.amazonaws.mobileconnectors.cognitoidentityprovider.util.CognitoJWTParser;
8788
import com.amazonaws.services.cognitoidentity.model.NotAuthorizedException;
8889
import org.json.JSONException;
@@ -934,6 +935,12 @@ public void onError(Exception error) {
934935
// global sign out by locally signing them out here.
935936
if (error instanceof NotAuthorizedException) {
936937
signOutLocally(options, onSuccess, onError);
938+
} else if (error instanceof AuthNavigationException) {
939+
// User cancelled the sign-out screen.
940+
onError.accept(new AuthException.UserCancelledException(
941+
"The user cancelled the sign-out attempt.", error,
942+
"To recover, catch this error, and retry sign-out."
943+
));
937944
} else {
938945
// Any other runtime exception means global sign out failed for another reason
939946
// (e.g. device offline), in which case we pass that error back to the customer.
@@ -989,6 +996,12 @@ public void onError(Exception error) {
989996
"Failed to sign out since Auth is already signed out",
990997
"No need to sign out - you already are!"
991998
));
999+
} else if (error instanceof AuthNavigationException) {
1000+
// User cancelled the sign-out screen.
1001+
onError.accept(new AuthException.UserCancelledException(
1002+
"The user cancelled the sign-out attempt.", error,
1003+
"To recover, catch this error, and retry sign-out."
1004+
));
9921005
} else {
9931006
onError.accept(new AuthException(
9941007
"Failed to sign out",
@@ -1061,13 +1074,17 @@ public void onResult(UserStateDetails details) {
10611074

10621075
@Override
10631076
public void onError(Exception error) {
1064-
onException.accept(
1065-
new AuthException(
1066-
"Sign in with web UI failed",
1067-
error,
1068-
"See attached exception for more details"
1069-
)
1070-
);
1077+
if (error instanceof AuthNavigationException) {
1078+
onException.accept(new AuthException.UserCancelledException(
1079+
"The user cancelled the sign-in attempt, so it did not complete.",
1080+
error, "To recover: catch this error, and show the sign-in screen again."
1081+
));
1082+
} else {
1083+
onException.accept(new AuthException(
1084+
"Sign-in with web UI failed", error,
1085+
"See attached exception for more details"
1086+
));
1087+
}
10711088
}
10721089
});
10731090
}

core/src/main/java/com/amplifyframework/auth/AuthException.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,23 @@ public FailedAttemptsLimitExceededException(Throwable cause) {
449449
}
450450
}
451451

452+
/**
453+
* Could not complete an action because it was cancelled by the user.
454+
*/
455+
public static class UserCancelledException extends AuthException {
456+
private static final long serialVersionUID = 1L;
457+
458+
/**
459+
* Constructs an {@link UserCancelledException}.
460+
* @param message Describes why the error has occurred
461+
* @param cause An underlying cause of the error
462+
* @param recoverySuggestion How to remedy the error, if possible
463+
*/
464+
public UserCancelledException(String message, Throwable cause, String recoverySuggestion) {
465+
super(message, cause, recoverySuggestion);
466+
}
467+
}
468+
452469
/**
453470
* Allows the user to specify whether guest access is enabled or not since this can affect which
454471
* recovery message should be included.

0 commit comments

Comments
 (0)