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

Having trouble getting data to be distinct per user #27

Open SteveALee opened 8 years ago

SteveALee commented 8 years ago

I'm using UserPools and have followed your example code here but when i have 2 users the data is shared between them. Obviously not what I want! For now I'm not using sync, just logging the users in and out and getting / putting.

My code is https://github.com/OpenDirective/brian/blob/master/src/js/drivers/providers/aws/sync-aws-cognito.js https://github.com/OpenDirective/brian/blob/master/src/js/drivers/providers/aws/sync-aws-cognito.js

thanks

itrestian commented 8 years ago

Hi Steve,

What data are you getting and putting? Are you using sync to do that?

The flow we support is to use User Pools to get an Id Token. Use that Id Token to get an authenticated Identity with Cognito Identity. Finally, use that authenticated identity with Sync to synchronize your data.

Ionut.

SteveALee commented 8 years ago

Thanks. I believe I am doing that but without any complete example I cannot be sure.

So I use the pools example to log in a user to userpool using a EU identity pool. In the final callback in the login process I create the credentials passing the logins key as shown in some examples I found ( some do it differently)

At this point I see 3 tokens in local storage and another value.

Then I call the set for sync with some data. Actually serialised javascript data, I set 2 different keys. I realise rhis is inefficient but will do for now.

I see extra keys in local storage 2 end with the dataset name. One starts with null and the other some sort of guide.

I then logout out the user and log in anothet. When I get the keys is see th e values from the 1st user whuch is obviously incorrect.

Im not calling sync to server but that can't be required as connectivity might be down

Thanjs

SteveALee commented 8 years ago

1 point of confusion is how the separate regions passed to the config object interplay with the region embedded in the identity ids. Plus if I need to use the same indents pool for bother cognito userpools and cognito sync. There are many components here and it's not clear how exactly to get your ducks in a row :)

SteveALee commented 8 years ago

@itrestian

I'm really stuck with getting a basic set / get to work. Any help will be gratefully received. Thanks

I'm following these docs http://docs.aws.amazon.com/cognito/latest/developerguide/synchronizing-data.html

my code is

    AWS.config.region = AWSCONFIG.SyncIdentityRegion
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
      IdentityPoolId: AWSCONFIG.SyncIdentityPoo
    })
    const client = new AWS.CognitoSyncManager()
    client.openOrCreateDataset('dataset', (err, dataset) => {
      if (err) {
        console.error('Cognito sync error - Dataset', err)
      }
      console.debug('dataset', dataset)

      dataset.put('sss', '123', (err, record) => {
        if (err) {
          console.error('Cognito sync error - Put', err)
          return
        }
        console.debug('Put', record)
      })

      dataset.get('sss', (err, value) => {
        if (err) {
          console.error('Cognito sync error - get', err)
          return
        }
        console.debug('Got', value)
      })
    })
  }

I always get undefined back from the get. I can see the data has been put in the local storage. Here's the console output

dataset a {MAX_RETRY: 3, datasetName: "dataset", provider: a.C…s.a.u…l.i…t.constructor, local: a, remote: a}

Put a {key: "sss", value: "123", syncCount: 0, lastModifiedDate: Wed May 25 2016 21:48:34 GMT+0100 (GMT Summer Time), lastModifiedBy: ""…}
deviceLastModifiedDate: Wed May 25 2016 21:48:34 GMT+0100 (GMT Summer Time)
key: "sss"
lastModifiedBy: ""
lastModifiedDate: Wed May 25 2016 21:48:34 GMT+0100 (GMT Summer Time)
modified: true
syncCount: 0
value: "123"

Got undefined

thanks

SteveALee commented 8 years ago

It seems strange that 'modifiedBy is "" I have set the the tokens via cognito. I have enable unauthenticated access in the identity pool. but also given my User Pool Details in case that is need. The pool log only shows unautheticated access.

    const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData)
    cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: () => {
        cognitoUser.getSession((err, result) => {
          if (!err && result) {
            // Add the User's Id Token to the Cognito credentials login map.
            let credentials = {
              IdentityPoolId: AWSCONFIG.IdentityPoolId,
              Logins: {}  // updated below
            }
            credentials.Logins[AWSCONFIG.loginCredentials] = result.getIdToken().getJwtToken()
            AWS.config.credentials = new AWS.CognitoIdentityCredentials(credentials)
          }
        })
      },
RWOverdijk commented 7 years ago

👍 examples would be cool. I think it's a shame that it took six months to add a label, and that now, four months later, there's still no resolution.