刷新 AWS Cognito ID 令牌时出现问题 [英] Problem refreshing the AWS Cognito ID Token

查看:37
本文介绍了刷新 AWS Cognito ID 令牌时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用适用于 javascript 的 AWS 开发工具包刷新 AWS Cognito ID 令牌.我们需要令牌 ID 自动刷新,而无需对我们的用户进行任何操作.我创建了以下函数,我们将检查身份验证后获取的过期时间,当当前时间接近过期时间时,我们将调用此函数.

I'm trying to refresh the AWS Cognito ID Token using the AWS SDK for javascript. We need the token ID to be refreshed automatically without any action with our users. I create the following function and we will check the expiration time that is fetched after authentication and when the current time is near expiration time, we will call this function.

refreshToken(success, failure) {
    var poolData = {
        UserPoolId: this.initializeData.UserPoolId,
        ClientId: this.initializeData.ClientId,
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
    var cognitoUser = userPool.getCurrentUser();

    cognitoUser.getSession(function (err, session) {
        if (err) {
            alert(err.message || JSON.stringify(err));
            return;
        }

        AWS.config.region = 'us-east-2';
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: 'myIdentityPoolId',
            Logins: {
                'cognito-idp.us-east-2.amazonaws.com/myPoolId': session.getIdToken().getJwtToken(),
            },
        });

        AWS.config.credentials.refresh(error => {
            if (error) {
                console.error(error);
            } else {
                var refresh_token = session.getRefreshToken();
                console.log('refresh started.');
                
                cognitoUser.refreshSession(refresh_token, (err, session) => {
                    if (err) {
                        console.log(err);
                    } else {
                        AWS.config.credentials.params.Logins[
                            'cognito-idp.us-east-2.amazonaws.com/myPoolId'
                        ] = session.getIdToken().getJwtToken();
                        AWS.config.Credentials.refresh(err => {
                            if (err) {
                                console.log(err);
                            } else {
                                console.log('TOKEN SUCCESSFULLY UPDATED');
                                console.log(cognitoUser.getSession().getJWTToken);
                            }
                        });
                    }
                });
            }
        });
    });
}

但我在控制台中收到以下错误.

But I received the following error in my console.

类型错误:AWS.config.Credentials 未定义
在以下行:
AWS.config.Credentials.refresh(err => {
我该怎么做才能解决问题?

TypeError: AWS.config.Credentials is undefined
on the following line:
AWS.config.Credentials.refresh(err => {
what should I do to solve the problem?

推荐答案

通过使用以下语句而不是使用 AWS.config.Credentials.refresh 来解决问题:

The problem is solved by using the following statement instead of using AWS.config.Credentials.refresh:

( < AWS.CognitoIdentityCredentials > myAwsConfig.credentials).refresh();

这是完整的代码,它可以刷新 AWS Cognito 用户的令牌 ID:

Here is the completed code that works and it refreshes the token ID of the AWS Cognito User:

  refreshToken(success, failure) {
    var poolData = {
      UserPoolId: this.initializeData.UserPoolId,
      ClientId: this.initializeData.ClientId,
    };
    var userPool = new CognitoUserPool(poolData);
    var cognitoUser = userPool.getCurrentUser();
    var currentSession = null;

    cognitoUser.getSession(function (err, session) {
      if (err) {
        alert(err.message || JSON.stringify(err));
        return;
      }
      currentSession = session;
    });

    if(!currentSession){
      failure("there is a problem with current session. try again later.");
    }

    AWS.config.region = 'us-east-2';
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: this.initializeData.IdentityPoolId,
        Logins: {
            'cognito-idp.us-east-2.amazonaws.com/poolId': currentSession.getIdToken().getJwtToken(),
        },
    });

    var refresh_token = currentSession.getRefreshToken(); 
      cognitoUser.refreshSession(refresh_token, (err, session) => {

        if (err) {
          failure(err);
        } else {

          var myAwsConfig = AWS.config;
          myAwsConfig.credentials.sessionToken = session.getIdToken().getJwtToken();

          ( < AWS.CognitoIdentityCredentials > myAwsConfig.credentials).refresh(err => {
            if (err) {
              failure(err);
            } else {
              success(session.getIdToken().getJwtToken());
            }
          });
        }
      });
  }

这篇关于刷新 AWS Cognito ID 令牌时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆