Skip to content

Commit

Permalink
fix(Auth): Fix for when loading credentials the success/error is fire…
Browse files Browse the repository at this point in the history
…d twice (#2184)
  • Loading branch information
gpanshu committed Dec 20, 2022
1 parent 3818cc7 commit ba16385
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Expand Up @@ -34,6 +34,8 @@ import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.core.Amplify
import com.amplifyframework.hub.HubChannel
import com.amplifyframework.hub.HubEvent
import com.amplifyframework.logging.AndroidLoggingPlugin
import com.amplifyframework.logging.LogLevel
import com.amplifyframework.testutils.HubAccumulator
import com.amplifyframework.testutils.Resources
import com.amplifyframework.testutils.Sleep
Expand Down Expand Up @@ -388,6 +390,7 @@ class PinpointAnalyticsInstrumentationTest {
setUniqueId()
Amplify.Auth.addPlugin(AWSCognitoAuthPlugin() as AuthPlugin<*>)
Amplify.addPlugin(AWSPinpointAnalyticsPlugin())
Amplify.Logging.addPlugin(AndroidLoggingPlugin(LogLevel.DEBUG))
Amplify.configure(context)
Sleep.milliseconds(COGNITO_CONFIGURATION_TIMEOUT)
synchronousAuth = SynchronousAuth.delegatingTo(Amplify.Auth)
Expand Down
Expand Up @@ -56,12 +56,11 @@ internal class CredentialStoreClient(configuration: AuthConfiguration, context:
) {
var capturedSuccess: Result<AmplifyCredential>? = null
var capturedError: Exception? = null
val token = StateChangeListenerToken()
var token: StateChangeListenerToken? = StateChangeListenerToken()
credentialStoreStateMachine.listen(
token,
token as StateChangeListenerToken,
{ storeState ->
logger.verbose("Credential Store State Change: $storeState")

when (storeState) {
is CredentialStoreState.Success -> {
capturedSuccess = Result.success(storeState.storedCredentials)
Expand All @@ -72,11 +71,13 @@ internal class CredentialStoreClient(configuration: AuthConfiguration, context:
is CredentialStoreState.Idle -> {
val success = capturedSuccess
val error = capturedError
if (success != null) {
credentialStoreStateMachine.cancel(token)
if (success != null && token != null) {
credentialStoreStateMachine.cancel(token!!)
token = null
onSuccess(success)
} else if (error != null) {
credentialStoreStateMachine.cancel(token)
} else if (error != null && token != null) {
credentialStoreStateMachine.cancel(token!!)
token = null
onError(error)
}
}
Expand Down

0 comments on commit ba16385

Please sign in to comment.