Please go to our Blogger site at blog.backspace.academy This site will no longer be updated.
Get CognitoSync Session Token
Now that we have our CognitoID credentials we can use these to access CognitoSync. First we need to use our new temporary credentials to create a CognitoSync session token.
We are going to create a new function to get our CognitoSync session token. Open index.js in your editor and add a call to the new function getCognitoSynToken() in the callback of getCognitoID().
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{
        AWS_TEMP_CREDENTIALS = AWS.config.credentials.data.Credentials;
COGNITO_IDENTITY_ID = AWS.config.credentials.identityId;
console.log("Cognito Identity Id: ".green + COGNITO_IDENTITY_ID);
getCognitoSynToken();
}
});
}
In order to get the token we must make a call to list records. If our dataset doesn't exist (as is the case now) it will be created automatically. We also get the sync count for the dataset which is needed later to add or change dataset records.
Now lets create the function:
function getCognitoSynToken(){
/* Other AWS SDKs will automatically use the Cognito Credentials provider */
/* configured in the JavaScript SDK. */
cognitosync = new AWS.CognitoSync();
cognitosync.listRecords({
DatasetName: COGNITO_DATASET_NAME, /* required */
IdentityId: COGNITO_IDENTITY_ID, /* required */
IdentityPoolId: COGNITO_IDENTITY_POOL_ID /* required */
}, function(err, data) {
if (err) console.log("listRecords: ".red + err, err.stack); /* an error occurred */
else {
console.log("listRecords: ".green + JSON.stringify(data));
COGNITO_SYNC_TOKEN = data.SyncSessionToken;
COGNITO_SYNC_COUNT = data.DatasetSyncCount;
console.log("SyncSessionToken: ".green + COGNITO_SYNC_TOKEN); /* successful response */
console.log("DatasetSyncCount: ".green + COGNITO_SYNC_COUNT);
}
});
}
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 318ms
GET / 200 17ms - 338b
GET /stylesheets/style.css 304 2ms
GET /auth/facebook 302 4ms - 388b
GET /auth/facebook/callback?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 302 348ms - 72b
FACEBOOK_TOKEN:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
GET /success 304 17ms
Cognito Identity Id: us-east-1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
listRecords: {"Count":1,"DatasetDeletedAfterRequestedSyncCount":false,"DatasetExists":true,"DatasetSyncCount":1,"LastModifiedBy":"XXXXXXXXXXXX","Records":[{"DeviceLastModifiedDate":"2014-08-15T15:33:58.627Z","Key":"USER_ID","LastModifiedBy":"XXXXXXXXXX","LastModifiedDate":"2014-08-15T15:33:58.627Z","SyncCount":1,"Value":"XXXXXXXXXXXX"}],"SyncSessionToken":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
SyncSessionToken: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DatasetSyncCount: 1
Now that we have our CognetoSync session token we can use this to add records to our dataset.
Like this tutorial? Please click the share buttons to tell others.
Next - Add Records to CognitoSync Dataset