Advertisement
Guest User

Untitled

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