Guest User

Untitled

a guest
Sep 9th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. const ACI = require("amazon-cognito-identity-js");
  2. const AWS = require("aws-sdk");
  3.  
  4. exports.handler = (event, context, callback) => {
  5.  
  6. var authenticationData = {
  7. Username : event.username ? event.username : "",
  8. Password : event.password ? event.password : "",
  9. };
  10. var authenticationDetails = new ACI.AuthenticationDetails(authenticationData);
  11.  
  12. var poolData = {
  13. UserPoolId : 'eu-west-2_hwI4lvY1T', // Your user pool id here
  14. ClientId : '4e20kvmotvvbgc79srv3u682fn' // Your client id here
  15. };
  16. var userPool = new ACI.CognitoUserPool(poolData);
  17.  
  18. var userData = {
  19. Username : event.username ? event.username : "",
  20. Pool : userPool
  21. };
  22. var cognitoUser = new ACI.CognitoUser(userData);
  23.  
  24. cognitoUser.authenticateUser(authenticationDetails, {
  25. onSuccess: function (result) {
  26. var accessToken = result.getIdToken().getJwtToken();
  27.  
  28. callback(accessToken);
  29. return;
  30. var attributeList = [];
  31. var attribute = {
  32. Name : 'custom:x-api-key',
  33. Value : 'DYGbjx1IEc4xAwK4oOQN959SfrJDxkXKaFvjBO1S'
  34. };
  35. var attribute = new ACI.CognitoUserAttribute(attribute);
  36. attributeList.push(attribute);
  37.  
  38. cognitoUser.updateAttributes(attributeList, function(err, result) {
  39. if (err) {
  40. console.log("ERR" + err.message || JSON.stringify(err));
  41. return;
  42. }
  43. console.log('call result: ' + result);
  44. });
  45.  
  46. return;
  47.  
  48. // Initialize the Amazon Cognito credentials provider
  49. AWS.config.region = 'eu-west-2'; // Region
  50. AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  51. IdentityPoolId: 'eu-west-2:2bc384fb-f85a-46bd-81ee-b3b6d86834bc',
  52. Logins: {
  53. 'cognito-idp.eu-west-2.amazonaws.com/eu-west-2_hwI4lvY1T': accessToken
  54. }
  55. });
  56.  
  57. AWS.config.getCredentials((error) => {
  58. if (error) {
  59. callback("Error: " + error.message || JSON.stringify(error), null);
  60. } else {
  61. callback(null, AWS.config.credentials.data);
  62. }
  63. });
  64.  
  65. },
  66.  
  67. onFailure: function(err) {
  68. callback("Error: " + err.message || JSON.stringify(err), null);
  69. },
  70.  
  71. });
  72.  
  73. };
  74.  
  75. exports.handler({username: "xAdlerTest2", password: "password"}, {}, callbacks);
  76.  
  77. function callbacks(s, a) {
  78. console.log(s || a);
  79. }
Add Comment
Please, Sign In to add comment