0

** Here is the code for cognito-user and I have to use the token value in another file, so how can I use, how can I export token value?**

    const {
    AuthenticationDetails,
    CognitoUser,
    CognitoUserPool
} = require("amazon-cognito-identity-js");

var poolData = {
    UserPoolId : '', // your user pool id here
    ClientId : '' // your client id here
};

var userPool = new CognitoUserPool(poolData);

var userData = {
    Username : '', // your username here
    Pool : userPool
};

var cognitoUser = new CognitoUser(userData);

var authenticationData = {
    Username: '',
    Password: '',
};

var authenticationDetails = new AuthenticationDetails(authenticationData);

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess:function(data) {
        var token = data.getIdToken().getJwtToken()
        console.log("user",token) 
        
    } 
})
1111
  • 1
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 03 '22 at 23:02

1 Answers1

0

use Promise, like this;

var details = new Promise(async (resolve, reject) => {
  const {
    AuthenticationDetails,
    CognitoUser,
    CognitoUserPool
  } = require("amazon-cognito-identity-js");

  var poolData = {
    UserPoolId: '', // your user pool id here
    ClientId: '' // your client id here
  };

  var userPool = new CognitoUserPool(poolData);

  var userData = {
    Username: '', // your username here
    Pool: userPool
  };

  var cognitoUser = new CognitoUser(userData);

  var authenticationData = {
    Username: '',
    Password: '',
  };

  var authenticationDetails = new AuthenticationDetails(authenticationData);

  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (data) {
      var token = data.getIdToken().getJwtToken()
      console.log("user", token)
      resolve(token)
    }
  })
})