Jiew Meng
Jiew Meng

Reputation: 88197

Cognito: How to correctly implement enhanced simplified authentication flow

I am trying to get the enhanced simplified authentication flow described on AWS docs

enter image description here

Problem is I can't figure out how to correctly use the SDK ...

  AWS.config.region = "ap-northeast-2"
  const cognitoParams = {
    IdentityPoolId: "ap-northeast-2:...",
    Logins: {
      "accounts.google.com": googleUser.getAuthResponse().id_token
    }
  }
  AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

  const identity = new AWS.CognitoIdentity()
  identity.getId(cognitoParams, function (err, identityId) {
    console.log(identityId)

    const identityParams = Object.assign({}, cognitoParams, {
      IdentityId: identityId
    })

    identity.getCredentialsForIdentity(identityParams, function (err, data) {
      console.log(data)
    })
  })

The 2 console.log gives null

AWS.config.region = "ap-northeast-2"
const cognitoParams = {
  IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745",
  Logins: {
    "accounts.google.com": googleUser.getAuthResponse().id_token
  }
}
AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

AWS.config.credentials.getId(function (err, identityId) {
  console.log(identityId)

  const identityParams = Object.assign({}, cognitoParams, {
    IdentityId: identityId
  })

  AWS.config.credentials.getCredentialsForIdentity(identityParams, function (err, data) {
    console.log(data)
  })
})

The above gives me the identity but fails with Cannot read property 'getCredentialsForIdentity' of undefined.

How do I implement this?

Upvotes: 1

Views: 841

Answers (1)

Jiew Meng
Jiew Meng

Reputation: 88197

I found that the below works ... I should be calling functions from an instance of CognitoIdentity rather than CognitoIdentityCredentials ... but it isn't clear in the documentations.

In fact it uses CognitoIdentityCredentials and reason for that? When do I use either?

  AWS.config.region = "ap-northeast-2"
  const cognitoParams = {
    IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745",
    Logins: {
      "accounts.google.com": googleUser.getAuthResponse().id_token
    }
  }
  // AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

  const identity = new AWS.CognitoIdentity()
  identity.getId(cognitoParams, function (err, identityData) {
    if (err) {
      return console.error(err)
    }

    const identityParams = {
      IdentityId: identityData.IdentityId,
      Logins: cognitoParams.Logins
    }

    identity.getCredentialsForIdentity(identityParams, function (err, data) {
      if (err) {
        return console.error(err)
      }
      console.log(data)
    })

Upvotes: 1

Related Questions