diff --git a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt index 1eee525cf..17acdf184 100644 --- a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt +++ b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt @@ -1880,6 +1880,7 @@ internal class RealAWSCognitoAuthPlugin( private fun _signOut(sendHubEvent: Boolean = true, onComplete: Consumer) { val token = StateChangeListenerToken() + var cancellationException: UserCancelledException? = null authStateMachine.listen( token, { authState -> @@ -1921,6 +1922,18 @@ internal class RealAWSCognitoAuthPlugin( ) ) } + authNState is AuthenticationState.SigningOut -> { + val state = authNState.signOutState + if (state is SignOutState.Error && state.exception is UserCancelledException) { + cancellationException = state.exception + } + } + authNState is AuthenticationState.SignedIn && cancellationException != null -> { + authStateMachine.cancel(token) + cancellationException?.let { + onComplete.accept(AWSCognitoAuthSignOutResult.FailedSignOut(it)) + } + } else -> { // No - op } diff --git a/aws-auth-cognito/src/main/java/com/amplifyframework/statemachine/codegen/states/SignOutState.kt b/aws-auth-cognito/src/main/java/com/amplifyframework/statemachine/codegen/states/SignOutState.kt index 376c3ef0e..1cd3ca2f2 100644 --- a/aws-auth-cognito/src/main/java/com/amplifyframework/statemachine/codegen/states/SignOutState.kt +++ b/aws-auth-cognito/src/main/java/com/amplifyframework/statemachine/codegen/states/SignOutState.kt @@ -15,6 +15,7 @@ package com.amplifyframework.statemachine.codegen.states +import com.amplifyframework.auth.cognito.exceptions.service.UserCancelledException import com.amplifyframework.auth.cognito.isAuthEvent import com.amplifyframework.auth.cognito.isSignOutEvent import com.amplifyframework.statemachine.State @@ -86,7 +87,15 @@ internal sealed class SignOutState : State { } is SignOutEvent.EventType.UserCancelled -> { val action = signOutActions.userCancelledAction(signOutEvent) - StateResolution(Error(Exception("User Cancelled")), listOf(action)) + StateResolution( + Error( + UserCancelledException( + "The user cancelled the sign-out attempt, so it did not complete.", + "To recover: catch this error, and attempt the sign out again." + ) + ), + listOf(action) + ) } else -> defaultResolution }