AWS Free Certification Courses

Please go to our Blogger site at blog.backspace.academy This site will no longer be updated.

Get CognitoID Credentials

 

Now it's time to pass our Facebook token over to Cognito. Prior to version v2.0.14 of the AWS Javascript SDK this was a difficult process involving calls to IAM and STS. A new object CognitoIdentityCredentials has greatly simplified the CognitoID credentials process by removing the need to create STS tokens and temporary IAM credentials yourself.

We are going to create a new function to get our CognitoID credentials. Open index.js in your editor and add a call to the new function getCognitoID() in the callback of our success page.

/* GET Facebook success page. */

router.get('/success', function(req, res, next) {

  console.log('FACEBOOK_TOKEN:'.green + FACEBOOK_TOKEN); 

  getCognitoID();

  res.send('Logged in as ' + FACEBOOK_USER.name + ' (id:' + FACEBOOK_USER.id + ').');

});

Now lets create the function:

function getCognitoID(){

  var params = {

    AccountId: AWS_ACCOUNT_ID, /* required */

    RoleArn: IAM_ROLE_ARN,  /* required */

    IdentityPoolId: COGNITO_IDENTITY_POOL_ID, /* required */

    Logins: {

      'graph.facebook.com': FACEBOOK_TOKEN

    } 

  };

  AWS.config.region = AWS_Region;

  /* initialize the Credentials object */

  AWS.config.credentials = new AWS.CognitoIdentityCredentials(params);

  /* Get the credentials for our user */

  AWS.config.credentials.get(function(err) {

    if (err) console.log("credentials.get: ".red + err, err.stack); /* an error occurred */

      else{

&nbsp &nbsp &nbsp &nbsp AWS_TEMP_CREDENTIALS = AWS.config.credentials.data.Credentials;

        COGNITO_IDENTITY_ID = AWS.config.credentials.identityId;

        console.log("Cognito Identity Id: ".green + COGNITO_IDENTITY_ID);

      }

  });

}

Now run the app with npm start again and you should get something like the following from the console after you have logged in from the browser.

GET / 304 322ms

GET /stylesheets/style.css 304 3ms

GET /auth/facebook 302 4ms - 388b

GET /auth/facebook/callback?code=XXXXXXXXXXXXXX_-XXXXXXXXX-XXXXXXXXXXXXX_XXXXXXXXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXX_XXXXXXXXXX_XXX--XXX-XXXXXXXXXXX-XXXXX-XXXXX_XXXXXXXXXXXXXXX-XXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXX 302 327ms - 72b

FACEBOOK_TOKEN:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

GET /success 304 36ms

Cognito Identity Id: us-east-1:XXXXXXX-XXXX-XXX-XXX-XXXXXXXX

Now that we have our CognetoID credentials we can get our CognetoSync session token.

Like this tutorial? Please click the share buttons to tell others.

 

Next - Get CognitoSync Session Token

 

 

AWS Free Certification Courses