【问题标题】:how to get awstoken from AWS cognito如何从 AWS cognito 获取 awstoken
【发布时间】:2021-11-19 20:00:04
【问题描述】:

我想在成功函数之外使用 accessToken 变量。我尝试了不同的方式来使用变量,但没有奏效。

var authenticationData = {
  Username : 'username',
  Password : 'password',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1_ExaMPle',
  ClientId : '1example23456789'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
  Username : 'username',
  Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess: function (result) {
    var accessToken = result.getAccessToken().getJwtToken();

    /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
    var idToken = result.idToken.jwtToken;
  },

  onFailure: function(err) {
    alert(err);
  },
});

【问题讨论】:

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


    【解决方案1】:

    为什么不将accessToken 添加到authenticationData 对象中?

    var authenticationData = {
      Username : 'username',
      Password : 'password',
      AccessToken : ''
    };
    
    // And then set the token in the success method:
    authenticationData.AccessToken = result.getAccessToken().getJwtToken();
    
    

    一旦设置了authenticationData.AccessToken,您就可以在范围内的任何地方使用该值。

    【讨论】:

    • 感谢您的回答。这对我不起作用。我尝试运行上面提到的,但问题是如何在类外访问令牌,因为如果我在成功方法之外运行 console.log(authenticationData.AccessToken),结果是未定义的。
    • 不管你把token放在authenticationData对象还是不同的对象里,概念还是一样的,re:把token保存到当前作用域之外的东西,这样就可以在里面访问了后续方法。
    猜你喜欢
    • 2017-05-27
    • 2019-05-20
    • 2018-09-18
    • 1970-01-01
    • 2022-11-22
    • 2022-11-07
    • 2019-12-23
    • 2021-03-09
    • 2019-03-23
    相关资源
    最近更新 更多