amazon-cognito-js
amazon-cognito-js copied to clipboard
Sync unauthenticated user data with just authenticated user
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?
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.