Skip to content

fix(auth): Fix for when loading credentials the success/error is fired twice #2184

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

Merged
merged 6 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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