Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. var AWS = require('aws-sdk');
  2. var AWSCognito = require('amazon-cognito-identity-js');
  3. var util = require('util');
  4.  
  5. var params = {
  6. Username: '',
  7. Password: '',
  8. UserPoolId: 'us-west-2_aLI134pRo',
  9. ClientId: '3jmku5aeaqe6pdkqa5q18trjk5',
  10. IdentityPoolId: 'us-west-2:93be5994-d1aa-4e3a-b088-3ed28fa4b068',
  11. AWSRegion: 'us-west-2'
  12. }
  13.  
  14. AWS.config.update({region: params.AWSRegion});
  15.  
  16. var authenticationData = {
  17. Username : params.Username,
  18. Password : params.Password
  19. };
  20. var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
  21.  
  22. var poolData = {
  23. UserPoolId : params.UserPoolId,
  24. ClientId : params.ClientId
  25. };
  26. var userPool = new AWSCognito.CognitoUserPool(poolData);
  27.  
  28.  
  29. var userData = {
  30. Username : params.Username,
  31. Pool : userPool
  32. };
  33. var cognitoUser = new AWSCognito.CognitoUser(userData);
  34.  
  35. cognitoUser.authenticateUser(authenticationDetails, {
  36.  
  37. onSuccess: function (result) {
  38. console.log("User Authenticated !");
  39. cognitoUserPoolLoginProvider = 'cognito-idp.' + params.AWSRegion + '.amazonaws.com/' + params.UserPoolId;
  40. var logins = {};
  41. logins[cognitoUserPoolLoginProvider] = result.getIdToken().getJwtToken();
  42.  
  43. AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  44. IdentityPoolId : params.IdentityPoolId, // your identity pool id here
  45. Logins : logins
  46. });
  47.  
  48. AWS.config.credentials.get(function(err) {
  49. if (err) {
  50. callback(err, null);
  51. } else {
  52. var creds = {
  53. AccessKeyId: AWS.config.credentials.accessKeyId,
  54. SecretAccessKey: AWS.config.credentials.secretAccessKey,
  55. SessionToken: AWS.config.credentials.sessionToken,
  56. }
  57. }
  58.  
  59. var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
  60.  
  61.  
  62.  
  63. cognitoidentityserviceprovider.listUsers({UserPoolId: params.UserPoolId}, function(err, data) {
  64. if (err) {
  65. console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
  66. } else {
  67. console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
  68. }
  69. });
  70.  
  71.  
  72. var dynamodb = new AWS.DynamoDB();
  73. var docClient = new AWS.DynamoDB.DocumentClient();
  74.  
  75. var readParams = {
  76. TableName: "Movies",
  77. Key:{
  78. "year": 2015,
  79. "title": "The Big New Movie"
  80. }
  81. };
  82.  
  83. docClient.get(readParams, function(err, data) {
  84. if (err) {
  85. console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
  86. } else {
  87. console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
  88. }
  89. });
  90.  
  91. });
  92. },
  93.  
  94. onFailure: function(err) {
  95.  
  96. console.log("Error authenticating ! Trying an API call anyway.... (will fail)");
  97.  
  98. var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
  99.  
  100. cognitoidentityserviceprovider.listUsers({UserPoolId: params.UserPoolId}, function(err, data) {
  101. if (err) {
  102. console.log(err, err.stack);
  103. } else {
  104. console.log(data);
  105. }
  106. });
  107.  
  108. },
  109.  
  110. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement