Describe the bug
We are using Cognito and AWSMobileClient to authenticate users in one of our apps. This is a code snippet showing how we are authenticating the user:
After sign in the user, we perform an update of its attributes in order to update its nickname. After this step, idToken and accessToken from AWSMobileClient.getTokens() method are nil.
I have debugged the SDK and the problem is caused by updateAttributes method of AWSCognitoIdentityUser class. When this method is executed, the clearSession method is called, causing accessToken and idToken to be nil.
The problem happens when you try to retrieve the new tokens. The method used to achieve it is getSessionInternal of AWSCognitoAuth class. In this method, the access token and id token are only updated if the expiration date has arrived. This cause the tokens to remain nil after and updateAttributes
To Reproduce
Steps to reproduce the behavior:
1.- Create a sample app.
2.- Include these dependencies in your podfile
```
pod 'AWSAPIGateway'
pod 'AWSMobileClient'
pod 'AWSAuthUI'
pod 'AWSUserPoolsSignIn'
pod 'AWSAppSync'
pod 'AWSPinpoint'
3.- In your app delegate initialize the AWSMobileClient
AWSMobileClient.sharedInstance().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState)")
}
if let error = error {
print("Error: \(error)")
}
}
4.- Sign in using Hosted UI
let hostedUIOptions : HostedUIOptions? = HostedUIOptions(idpIdentifier: "idpidentifier ")
AWSMobileClient.sharedInstance().showSignIn(navigationController: self.navigationController!, hostedUIOptions: hostedUIOptions) { (userState, error) in
if let error2 = error as? AWSMobileClientError {
print(error2.localizedDescription)
}
if let userState = userState {
print("Status: (userState.rawValue)")
}
}
5.- [THIS IS THE KEY STEP] Update user attributes:
let pool = AWSCognitoIdentityUserPool.default()
let nicknameAttribute = AWSCognitoIdentityUserAttributeType(name: "nickname", value: newNickname)
pool.currentUser()?.update([nicknameAttribute]).continueWith(block: { (task) -> Any? in
DispatchQueue.main.async {
if task.error == nil {
onResult(.success(()))
} else {
onResult(.failure(MigoError(type: UserError.Unknown)))
}
}
})
6.- Try to get tokens
AWSMobileClient.sharedInstance().getTokens { (tokens, error) in
// tokens.idToken? is nil
// tokens.accessToken is nil
}
```
Which AWS service(s) are affected?
AWSCognitoAuth, AWSMobileClient
Expected behavior
I expect idTokenand accessToken to have value
Environment(please complete the following information):
Device Information (please complete the following information):
@JesusMartinAlonso Thanks for reporting and for the detailed analysis and PR. We'll take a look at this and update as soon as we have more info.
Hi here!
@palpatim any updates on this issue?
it is not possible for us to use the SDK with this bug.
When do you plan to have a fix?
Hi @VitaliiHudenko. I submitted a Pull Request #1734 that solves the issue.
If you use cocoapods you can use my solution until the AWS team approve it:
pod 'AWSMobileClient', :git => 'https://github.com/JesusMartinAlonso/aws-sdk-ios.git', :branch => 'master'
Thank you @JesusMartinAlonso for the PR. This has been merged to develop branch and will be available in the next release.
This was released in 2.12.0. Please let us know if you have any issues with this.
This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.
Most helpful comment
Thank you @JesusMartinAlonso for the PR. This has been merged to develop branch and will be available in the next release.