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
180 stars 113 forks source link

Mismatched key prefix when caching and restoring device #255

Closed benja8151 closed 1 month ago

benja8151 commented 4 months ago

I've noticed that whenever I login (with authenticateUser) a new device is registered in Cognito. I found out that keyPrefix used in cacheDeviceKeyAndPassword and getCachedDeviceKeyAndPassword functions is not the same. This means that getCachedDeviceKeyAndPassword function never successfully restores cached device.

For illustration, I printed out keyPrefix used in both functions:

getCachedDeviceKeyAndPassword KEY PREFIX: CognitoIdentityServiceProvider.<clientID>.test.user@email.com

cacheDeviceKeyAndPassword KEY PREFIX: CognitoIdentityServiceProvider.<clientID>.404fa7dd-29f4-462f-b746-8e453f8fe3a8

The end of key prefix is email address in getCachedDeviceKeyAndPassword and Cognito username in getCachedDeviceKeyAndPassword.

I'm using email as username when creating CognitoUser on login, like so:

CognitoUser user = CognitoUser(email, _cognitoUserPool!)

Could this be a problem, since keyPrefix is constructed from username, and I'm guessing username is set to email address when first calling getCachedDeviceKeyAndPassword :

  String get keyPrefix => 'CognitoIdentityServiceProvider.${pool.getClientId()}.$username';
benja8151 commented 4 months ago

I think https://github.com/furaiev/amazon-cognito-identity-dart-2/commit/08e43d6686931d2890816cc3a86e83910e8b1c4a might be linked to this, because it seems email in username field in CognitoUser is replaced by user id in this step. Perhaps we could store original username (or key prefix) separately, for later use?

benja8151 commented 4 months ago

I prepared a quick PR where I store original email separately from username, but I don't have much experience with this package, so I'm not sure if it breaks some other flow. Login, session refresh and restore seem to work correctly though: https://github.com/furaiev/amazon-cognito-identity-dart-2/pull/257

furaiev commented 4 months ago

@benja8151 you are correct, in the original package the keyPrefix consist username or not from time to time. We need to copy this logic, which should fix the issue. Can you please update your PR based on amplify-js?

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 30 days with no activity.

anthonycastelli commented 2 months ago

Has there been an update on this? I'm now running into a similar issue

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 month ago

This issue was closed because it has been inactive for 14 days since being marked as stale.