amplify-swift
amplify-swift copied to clipboard
Auth.getCurrentUser() returning nil when currentUserState is "signedIn"
Describe the bug
Our iOS app sometimes encounters AuthError.invalidState errors when users are attempting to sign in via Cognito.
In particular there has been a spike with the release of the new iPhone 14 devices (leading us to suspect a potential data migration issue from older devices).
Steps To Reproduce
We have so far been unable to reproduce ourselves but the errors have been logged by users of our app. The steps we can observe are as follows:
- Call Auth.getCurrentUser()
- Returns nil, which we take to mean the user is not signed in
- At this stage it seems teh underlying AWSMobileClient is reporting currentUserState = signedIn
- Call Auth.signIn( ... )
- Fails with invalidState ("There is already a user which is signed in. Please log out the user before calling showSignIn.")
We don't log any additional context to the invalidState error, which appears to be derived from AWSMobileClientError.invalidState
Expected behavior
We expect the call to Auth.SignIn() to succeed if Auth.getCurrentUser() returns nil, but in some cases it is not.
Amplify Framework Version
1.28.0
Amplify Categories
Auth
Dependency manager
Swift PM
Swift version
5.6
CLI version
N/A
Xcode version
13.4
Relevant log output
No response
Is this a regression?
No
Regression additional context
We have seen the error slightly more in 1.28.0 than we did previously in 1.18.1, but it does seem to have been present at a low level for a while.
Device
Various. Lots of recent iPhone 14 occurrances
iOS Version
14, 15, 16
Specific to simulators
No response
Additional context
No response
I've updated the description above following our discovery that Auth.getCurrentUser() is returning nil when the underlying AWSMobileClient is reporting currentUserState = signedIn
@knellr I want to get some information on a few things
- Does this only happen to users who have updated to iOS 16 or the new iPhone 14? And when you say data migration, you mean during an iOS upgrade?
- Can you share the sign in method your users are using? i.e. SRP, HostedUI, ?
- Also can you share the configuration that was used for Amplify redacted all the sensitive information?
@knellr In addition to the questions above, are you using a Cognito User Pool for sign in or are you using federatedSignIn via the escape hatch. The latter will have a nil user.
This issue may be related to device migration via device to device transfer, iCloud restore, and/or backup/restore on a Mac. We will need to investigate further and attempt to reproduce the issue.
In the meantime, a potential work-around would be to detect this state by checking to if both Auth.getCurrentUser() == nil
and Auth.fetchAuthSession() -> isLoggedIn == true
and then call Amplify.Auth.signOut()
to return to a proper signed out state.
Note for the amplify-ios team: more information was provided and is available on this SIM: V726012039