Skip to content

Instantly share code, notes, and snippets.

@eks1985
Created May 12, 2017 13:56
Show Gist options
  • Save eks1985/af49b57b33c6010c6c4f46c098ecae4b to your computer and use it in GitHub Desktop.
Save eks1985/af49b57b33c6010c6c4f46c098ecae4b to your computer and use it in GitHub Desktop.
aws
const login = () => {

    let Username = 'reacttest';
    let Password = 'reacttest';

    let authenticationDetails = new AuthenticationDetails({
      Username,
      Password
    });

    const { UserPoolId, ClientId, region } = config;
    var poolData = { UserPoolId, ClientId, region  };
    var userPool = new CognitoUserPool(poolData);
    var userData = {
        Username : 'reacttest',
        Pool : userPool
    };
    var cognitoUser = new CognitoUser(userData);

    let sessionData;
    if (cognitoUser != null) {
        cognitoUser.getSession(function(err, session) {
            if (err) {
                alert(err);
                return;
            };
            sessionData = session;
            console.log('sessionData', sessionData);
            console.log('session validity: ' + session.isValid());
        });
    }

    if (sessionData)
      console.log('idToken =>>>', sessionData.idToken.getJwtToken());

    cognitoUser.authenticateUser(authenticationDetails, {

        onSuccess: function (result) {

          // get identity token
          // identityPoolId and poolData coming from config.js
          var params = {
            IdentityPoolId: config.IdentityPoolId,
            Logins: {
              [`cognito-idp.${config.region}.amazonaws.com/${config.UserPoolId}`]:
              result.getIdToken().getJwtToken()
            }
          };

          // get credentials
          AWS.config.credentials = new AWS.CognitoIdentityCredentials(params, function(err, data) {
            if (err) console.log(err, err.stack);
            else console.log('AWS.config.credentials', data);
          });

          AWS.config.credentials.get(function(err){
            if (err) {
              alert(err);
            } else {
              var accessKeyId = AWS.config.credentials.accessKeyId;
              var secretAccessKey = AWS.config.credentials.secretAccessKey;
              var sessionToken = AWS.config.credentials.sessionToken;

              console.log('accessKeyId =>>>', accessKeyId);
              console.log('secretKey =>>>', secretAccessKey);
              console.log('sessionToken =>>>', sessionToken);
              console.log('region =>>>', poolData.region);

              // eslint-disable-next-line
              var apigClient = apigClientFactory.newClient({
                accessKey: accessKeyId,
                secretKey: secretAccessKey,
                sessionToken: sessionToken,
                region: poolData.region
              });

              // here we could add an api call

              var params = {};
              var additionalParams = {
                headers: {
                  // Authorization: sessionData.idToken
                  Authorization: sessionData.idToken.getJwtToken()
                },
                queryParams: {}
              };
              var body = {};

              apigClient.identityGet(params, body, additionalParams)
                .then(function(result){
                  if (err) {
                    alert(err);
                  } else {
                    console.log(JSON.stringify(result.data));
                  }
                }).catch( function(result){
                  //This is where you would put an error callback
                  if (err) {
                    alert(err);
                  }
                });
              }
            });

        },
        onFailure: function(err) {
            alert(err);
        },
    });
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment