Created
September 21, 2016 12:49
-
-
Save nachoab/1427b6ebd4b7b7d09d7dd76d9ead4c00 to your computer and use it in GitHub Desktop.
lambda udpate records from a google user using cognito
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.auth = (event, context, callback) => { | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | |
IdentityPoolId: IdentityPoolId, | |
Logins: { | |
'accounts.google.com': event.id_token | |
} | |
}); | |
AWS.config.credentials.get(() =>{ | |
if (!AWS.config.credentials.accessKeyId) { | |
return callback(new Error('invalid tokenid ' + event.id_token)); | |
} | |
const cognitosync = new AWS.CognitoSync({apiVersion: '2014-06-30'}); | |
cognitosync.listRecords({ | |
DatasetName: 'userinfo', | |
IdentityId: AWS.config.credentials.identityId, | |
IdentityPoolId: IdentityPoolId, | |
}, (err, data) => { | |
if (err) { | |
callback(err); | |
} else { | |
/** | |
data = { | |
"Records": [ | |
{ | |
"Key": "test", | |
"Value": "test value", | |
"SyncCount": 1, | |
"LastModifiedDate": "2016-09-21T12:34:47.519Z", | |
"LastModifiedBy": "eu-west-1:be04407a-a0a4-436e-ae23-276f4599d219", | |
"DeviceLastModifiedDate": "2016-09-21T12:34:47.519Z" | |
} | |
] | |
} | |
*/ | |
cognitosync.updateRecords({ | |
DatasetName: 'userinfo', | |
IdentityId: AWS.config.credentials.identityId, | |
IdentityPoolId: IdentityPoolId, | |
SyncSessionToken: data.SyncSessionToken, | |
RecordPatches: [{ | |
Key: 'test', | |
Op: 'replace', | |
SyncCount: 0, | |
Value: 'test value' | |
} | |
] | |
}, (err, updatedRecords) => { | |
if (err) { | |
callback(err); | |
} | |
else { | |
callback(null, { | |
identityId: AWS.config.credentials.identityId | |
}); | |
} | |
}); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment