amazon-archives / amazon-cognito-js

Amazon Cognito Sync Manager for JavaScript
http://aws.amazon.com/cognito
Apache License 2.0
202 stars 83 forks source link

Sync unauthenticated user data with just authenticated user #59

Open pmbanugo opened 6 years ago

pmbanugo commented 6 years ago

My app allows user add data while unauthenticated with cognito sync. I want a situation where when a user logs in, it takes data it saved while user was unauthenticated and merges it with the data owned by the just authenticated user. How do I achieve this?

abuist commented 6 years ago

I had this issue last year and found that it is actually a Cognito Identity issue. When you call the AWS.CognitoIdentityCredentials with the new user authenticated user it allocated you a new IdentityId.

So I retrieve the previously stored IdentityId from localStorage using hwIdentity = AWS.config.credentials.identityId; before I start the authentication process.

Then when I I retrieve the new credentials I pass it it as part of the parameters as follows: AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 'IdentityPoolId': aws_cognito_identity_pool_id, 'IdentityId': hwIdentity, 'Logins': logins });

This causes CognitoIdentity to add the new user information to the Identity or merge Identities if their are 2, which will initiate the Cognito Sync merge process.

Hope this Helps.