Cognito:如何正确实现增强的简化身份验证流程

时间:2017-05-20 14:04:12

标签: javascript amazon-web-services amazon-cognito

我正在尝试获取AWS docs

中描述的增强型简化身份验证流程

enter image description here

问题是我无法弄清楚如何正确使用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)
    })
  })

2 console.log给出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)
  })
})

上面给出了我的身份,但失败了Cannot read property 'getCredentialsForIdentity' of undefined

我该如何实现?

1 个答案:

答案 0 :(得分:0)

我发现下面有效......我应该调用CognitoIdentity而不是CognitoIdentityCredentials的实例中的函数...但是在文档中并不清楚。

实际上它使用CognitoIdentityCredentials并说明原因?我什么时候使用?

  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)
    })