0

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

Cognito Identity ID我的问题是当我在另一个页面上时如何检索用户 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);
          }


        });


     }
4

1 回答 1

0

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

var identityId = AWS.config.credentials.identityId;

但是,凭据不会被缓存,您每次都需要获取凭据。

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

于 2016-08-28T01:02:10.747 回答