aws-amplify / aws-amplify/amplify-android

UserCancelledException

Open
#2,649 5 comments 0 reactions 1 assignee Claimed by @tylerjroach View on GitHub
auth bug
Dominant language
Java
Stars
287
Forks
132
Avg merge
2d 2h
Merged PRs (30d)
45

Description

### Before opening, please confirm:

- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-android/issues?q=is%3Aissue+) and [discussions](https://github.com/aws-amplify/amplify-android/discussions).

### Language and Async Model

Kotlin, Kotlin - Coroutines

### Amplify Categories

Authentication

### Gradle script dependencies

```groovy
// Put output below this line
implementation 'com.amplifyframework:aws-auth-cognito:2.12.0'
implementation 'com.amplifyframework:core:2.12.0'
implementation 'com.amazonaws:aws-android-sdk-auth-userpools:2.73.0'
implementation 'com.amazonaws:aws-android-sdk-auth-ui:2.73.0'

```

### Environment information

```
# Put output below this line
------------------------------------------------------------
Gradle 8.3
------------------------------------------------------------

Build time: 2023-08-17 07:06:47 UTC

Kotlin: 1.9.0
Groovy: 3.0.17

```

### Please include any relevant guides or documentation you're referencing

https://docs.amplify.aws/android/build-a-backend/auth/sign-in-with-web-ui/#launch-web-ui-sign-in

### Describe the bug

When using `Amplify.Auth.signInWithWebUI(activity)` after successfully logging out, I occasionally get the `UserCancelledException` with no interaction with webview window.

### Reproduction steps (if applicable)

1. Call the sign out method then, within the sign out callback, call the `signInWithWebUI` method.
2. See the `UserCancelledException` after going through this process a few times.

Note: This is not consistent as to when the exception is returned. Sometimes it takes a couple times, sometimes many more.

### Code Snippet

```java
// Put your code below this line.

fun signInWithWebUI(
activity: Activity,
callback: (String?, AmplifyException?) -> Unit,
) {
signOut { success, error ->

Amplify.Auth.signInWithWebUI(activity,
{
fetchCurrentAuthSession(callback)
},
{
Log.e("", "Signin failed", it)
}
)
}
}

fun signOut(callback: (success: Boolean, error: Exception?) -> Unit) {
Amplify.Auth.signOut { signOutResult ->
when (signOutResult) {
is AWSCognitoAuthSignOutResult.CompleteSignOut -> {
Log.d("QuickstartApp", "cognito signout success")
callback.invoke(true, null)
}
is AWSCognitoAuthSignOutResult.PartialSignOut -> {
signOutResult.hostedUIError?.let {
callback.invoke(false, it.exception)
Log.d("QuickstartApp", "hostedUI error: ${it.exception.localizedMessage}")
// Optional: Re-launch it.url in a Custom tab to clear Cognito web session.

}
signOutResult.globalSignOutError?.let {
Log.e("QuickstartApp", "GlobalSignOut Error", it.exception)
callback.invoke(false, it.exception)
// Optional: Use escape hatch to retry revocation of it.accessToken.
}
signOutResult.revokeTokenError?.let {
Log.e("QuickstartApp", "RevokeToken Error", it.exception)
callback.invoke(false, it.exception)
// Optional: Use escape hatch to retry revocation of it.refreshToken.
}
}
is AWSCognitoAuthSignOutResult.FailedSignOut -> {
Log.d("QuickstartApp", "FailedSignOut: ${signOutResult.exception.localizedMessage}")
callback.invoke(false, signOutResult.exception)
}
}
}
}

fun fetchCurrentAuthSession(callback: (String?, AmplifyException?) -> Unit) {
Amplify.Auth.fetchAuthSession(
{
val session = it as AWSCognitoAuthSession
val token = session.userPoolTokensResult.value?.idToken
Log.d("QuickstartApp", "fetchCurrentAuthSession: $token")
callback(token, null)
},
{
Log.e("QuickstartApp", "Failed to fetch auth session: ${it.localizedMessage}")
callback(null, it)
}
)
}
```

### Log output

```
// Put your logs below this line

```
Signin failed
UserCancelledException{message=The user cancelled the sign-in attempt, so it did not complete., cause=null, recoverySuggestion=To recover: catch this error, and show the sign-in screen again.}
at com.amplifyframework.auth.cognito.RealAWSCognitoAuthPlugin$handleWebUISignInResponse$1.invoke(RealAWSCognitoAuthPlugin.kt:928)
at com.amplifyframework.auth.cognito.RealAWSCognitoAuthPlugin$handleWebUISignInResponse$1.invoke(RealAWSCognitoAuthPlugin.kt:878)
at com.amplifyframework.statemachine.StateMachine$getCurrentState$1.invokeSuspend(StateMachine.kt:121)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
```
```

### amplifyconfiguration.json

val categoryConfig = JSONObject(
"{\n" +
" \"auth\": {\n" +
" \"plugins\": {\n" +
" \"awsCognitoAuthPlugin\": {\n" +
" \"IdentityManager\": {\n" +
" \"Default\": {}\n" +
" },\n" +
" \"CognitoUserPool\": {\n" +
" \"Default\": {\n" +
" \"PoolId\": \"${BuildConfig.AMPLIFY_POOL_ID}\",\n" +
" \"AppClientId\": \"${BuildConfig.AMPLIFY_CLIENT_ID}\",\n" +
" \"Region\": \"us-east-2\"\n" +
" }\n" +
" },\n" +
" \"Auth\": {\n" +
" \"Default\": {\n" +
" \"authenticationFlowType\": \"USER_SRP_AUTH\",\n" +
" \"OAuth\": {\n" +
" \"WebDomain\": \"${BuildConfig.AMPLIFY_WEB_DOMAIN}\",\n" +
" \"AppClientId\": \"${BuildConfig.AMPLIFY_CLIENT_ID}\",\n" +
" \"SignInRedirectURI\": \"xxxxxx://signin\",\n" +
" \"SignOutRedirectURI\": \"xxxxxx://signout\",\n" +
" \"Scopes\": [\n" +
" \"email\",\n" +
" \"openid\",\n" +
" \"profile\",\n" +
" \"aws.cognito.signin.user.admin\"\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}"
)

### GraphQL Schema

```graphql
// Put your schema below this line

```

### Additional information and screenshots

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.