Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. var data = {
  2. UserPoolId: <YOUR_USER_POOL_ID>,
  3. ClientId: <YOUR_USER_POOL_CLIENT_ID>,
  4. };
  5. var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(data);
  6. var cognitoUser = userPool.getCurrentUser();
  7.  
  8. try {
  9. if (cognitoUser != null) {
  10. cognitoUser.getSession(function(err, session) {
  11. if (err) {
  12. console.log(err);
  13. return;
  14. }
  15.  
  16. console.log('session validity: ' + session.isValid());
  17. console.log('session token: ' + session.getIdToken().getJwtToken());
  18.  
  19. AWS.config.region = '<YOUR_REGION>';
  20. AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  21. IdentityPoolId : '<YOUR_IDENTITY_POOL_ID>',
  22. Logins : {
  23. // Change the key below according to the specific region your user pool is in.
  24. `cognito-idp.${AWS.config.region}.amazonaws.com/${data.UserPoolId}` : session.getIdToken().getJwtToken()
  25. }
  26. });
  27.  
  28. AWS.config.credentials.get(function(err) {
  29. if (!err) {
  30. var id = AWS.config.credentials.identityId;
  31. console.log('Cognito Identity ID '+ id);
  32.  
  33. // Instantiate aws sdk service objects now that the credentials have been updated
  34. var docClient = new AWS.DynamoDB.DocumentClient({ region: AWS.config.region });
  35. var params = {
  36. TableName: '<YOUR_DYNAMODB_TABLE>',
  37. Item:{userid:id, status:<STATUS_CODE>}
  38. };
  39. docClient.put(params, function(err, data) {
  40. if (err)
  41. console.error(err);
  42. else
  43. console.log(data);
  44. });
  45. }
  46. });
  47. });
  48. } else {
  49. console.log(err);
  50. return;
  51. }
  52. } catch (e) {
  53. console.log(e);
  54. return;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement