Skip to content

Instantly share code, notes, and snippets.

@eks1985
Last active May 11, 2017 23:28
Show Gist options
  • Save eks1985/99215a8c820580cd847ac2ed149b4e11 to your computer and use it in GitHub Desktop.
Save eks1985/99215a8c820580cd847ac2ed149b4e11 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 } = config;
    var poolData = { UserPoolId, ClientId };
    var userPool = new CognitoUserPool(poolData);
    var userData = {
        Username : 'reacttest',
        Pool : userPool
    };
    var cognitoUser = new CognitoUser(userData);

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {

          AWS.config.credentials = new AWS.CognitoIdentityCredentials({
              IdentityPoolId : config.IdentityPoolId, // your identity pool id here
              Logins : {
                  ['cognito-idp.' + config.region + '.amazonaws.com/' + config.UserPoolId] : result.getIdToken().getJwtToken()
              }
          });

          AWS.config.credentials.refresh(function(){
            console.log(AWS.config.credentials);
            var apigClient = window.apigClientFactory.newClient({
                accessKey: AWS.config.credentials.accessKeyId,
                secretKey: AWS.config.credentials.secretAccessKey,
                sessionToken: AWS.config.sessionToken, //OPTIONAL: If you are using temporary credentials you must include the session token
                region: 'eu-west-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
            });
            var additionalParams = {
              headers: {
                  "Access-Control-Allow-Origin": "*",
                  "Access-Control-Allow-Methods": "*",
                  "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"

              },
            };
            apigClient.identityiamGet({},{},additionalParams).then(res => {
              console.log(res);
            })

          });

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