Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. const AWS = require('aws-sdk');
  2. const CognitoSDK = require('amazon-cognito-identity-js-node');
  3. AWS.CognitoIdentityServiceProvider.AuthenticationDetails = CognitoSDK.AuthenticationDetails;
  4. AWS.CognitoIdentityServiceProvider.CognitoUserPool = CognitoSDK.CognitoUserPool;
  5. AWS.CognitoIdentityServiceProvider.CognitoUser = CognitoSDK.CognitoUser;
  6.  
  7. const Username = 'testuser';
  8. const TempPassword = 'TemporaryPassword2!';
  9. const NewPassword = 'NewPassword@#@!19';
  10. const Email = 'some@email.com';
  11. const config = { region: 'us-east-1' };
  12. const UserPoolId = 'USER_POOL_ID_HERE';
  13. const ClientId = 'APP_CLIENT_ID_HERE'; // Your App client id (add via Console->Cognito User Pool)
  14.  
  15. const cognitoIdentityServiceProvider =
  16. new AWS.CognitoIdentityServiceProvider(config);
  17.  
  18.  
  19. const saveOrUpdateUser = (profile) => {
  20.  
  21. //User Pool
  22. const poolData = {
  23. UserPoolId : UserPoolId,
  24. ClientId : ClientId // Your App client id here
  25. };
  26. const userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  27.  
  28. //User
  29. const userParams = {
  30. Pool: userPool,
  31. Username: Username,
  32. };
  33. var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userParams);
  34.  
  35. //Authentication
  36. const authenticationData = {
  37. Username: Username,
  38. Password: NewPassword, //1st time use TempPassword
  39. }
  40. const authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
  41. var responseFunctions = {
  42. onSuccess: (result) => {
  43. console.log('IT WORKED!');
  44. console.log(result);
  45. },
  46. onFailure: (err) => {
  47. console.log('no go :(');
  48. console.log(err);
  49. }
  50. };
  51.  
  52. //newPasswordRequired has to be added separately because it sends responseFunctions to completeNewPasswordChallenge
  53. responseFunctions.newPasswordRequired = (userAttributes, requiredAttributes) => {
  54. delete userAttributes.email_verified;
  55. cognitoUser.completeNewPasswordChallenge(NewPassword, {email: Email}, responseFunctions)
  56. };
  57. cognitoUser.authenticateUser(authenticationDetails, responseFunctions);
  58. };
  59.  
  60. saveOrUpdateUser();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement