当我在不同的页面上时获取Cognito身份证

时间:2016-08-08 08:38:23

标签: facebook amazon-cognito amazon-cognito-facebook

我能够成功登录使用Facebook进行身份验证的用户。这工作正常,页面按预期重定向到另一个页面。代码如下。

我的问题是,当我在另一个页面上时,如何检索用户ID(或Cognito Identity ID)? (不在同一页面,但当我在另一页时)

AWS.config.region='us-east-1';
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({

            // either IdentityPoolId or IdentityId is required
            // See the IdentityPoolId param for AWS.CognitoIdentity.getID (linked below)
            // See the IdentityId param for AWS.CognitoIdentity.getCredentialsForIdentity
            // or AWS.CognitoIdentity.getOpenIdToken (linked below)
            IdentityPoolId: CognitoIdentityPoolId,

            // optional, only necessary when the identity pool is not configured
            // to use IAM roles in the Amazon Cognito Console
            // See the RoleArn param for AWS.STS.assumeRoleWithWebIdentity (linked below)
            RoleArn: 'arn:aws:iam::9287589741169:role/Cognito_111Auth_Role',

            // optional tokens, used for authenticated login
            // See the Logins param for AWS.CognitoIdentity.getID (linked below)
            Logins: {
              'graph.facebook.com': accessToken
            },

            // optional name, defaults to web-identity
            // See the RoleSessionName param for AWS.STS.assumeRoleWithWebIdentity (linked below)
            RoleSessionName: 'web',

            expired: 'true'



        });
        console.log('Refreshing cognito credentials');
        AWS.config.credentials.refresh(function(err) {
          if (err) {
              console.log('Failed to refresh');
              return;
          } else {
              AWS.config.credentials.get(function(err){
                  if (!err) {
                    var id = AWS.config.credentials.identityId;
                    console.log('Cognito Identity ID '+ id);
                  }
              });

              var s3 = new AWS.S3();
              console.log('Creds '+ s3.config.credentials.sessionToken);
          }


        });


     }

1 个答案:

答案 0 :(得分:0)

身份ID在页面加载之间缓存,您可以检索它

var identityId = AWS.config.credentials.identityId;

然而,

凭据缓存,您每次都需要获取凭据。

此论坛帖子可能有所帮助: https://forums.aws.amazon.com/thread.jspa?threadID=179420&tstart=25