furaiev / amazon-cognito-identity-dart-2

Unofficial Amazon Cognito Identity Provider Dart SDK, to easily add user sign-up and sign-in to your mobile and web apps with AWS.
MIT License
187 stars 114 forks source link

Null credentials when authenticated #63

Closed salvatore373 closed 4 years ago

salvatore373 commented 4 years ago

After authenticating and using the Cognito credentials for a little time (along with other AWS services), when I try to retrieve them after an app hot restart, I get the following error and I'm no more able to use them to access the other AWS services.

Failed assertion: line 27 pos 12: 'accessKey != null': is not true.

However, I noticed that the getAwsCredentials() method gets called twice: while the first time it returns null keys and tokens, the second time they are correct. If I hot reload after the hot restart that gets me null keys, I get valid keys and tokens at the first call. I am pretty new to this package, so I might be making mistakes in my code, even though it is very basic. Here it is:

Future<CognitoCredentials> getCredentials() async {
    if (_cognitoUser == null || _session == null) return null;

    _credentials = CognitoCredentials(_identityPoolId, _userPool);
    await _credentials.getAwsCredentials(_session.getIdToken().getJwtToken());
    return _credentials;
}

Am I wrong with my code or is there an error in the package?

furaiev commented 4 years ago

Have you checked that your getCredentials isn't called twice? This part of your code seems correct.

salvatore373 commented 4 years ago

Yes, @furaiev, you were right. I called getCredentials() twice, one time immediately after the other. But why doesn't this function just return the user's credentials both the times?

furaiev commented 4 years ago

@salvatore373 what do you expect calling getAwsCredentials ?

salvatore373 commented 4 years ago

In my opinion, whenever you call getAwsCredentials it should return the credentials when they're available, and when they aren't it should return a null value or throw an exception. I think that this method should never return an object with null properties.

furaiev commented 4 years ago

getAwsCredentials is a VOID method. It doesn't return data.