Guest User

Untitled

a guest
Dec 30th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import AWS from 'aws-sdk/global';
  2. import S3 from 'aws-sdk/clients/s3';
  3. import {
  4. AuthenticationDetails,
  5. CognitoUser,
  6. CognitoUserPool,
  7. } from 'amazon-cognito-identity-js';
  8.  
  9. const REGION = 'some-string-value';
  10. const USER_POOL_ID = 'some-string-value';
  11. const IDENTITY_POOL_ID = 'some-string-value';
  12. const APP_CLIENT_ID = 'some-string-value';
  13. const POOL_KEY = `cognito-idp.${REGION}.amazonaws.com/${USER_POOL_ID}`;
  14.  
  15. let Username = 'some-string-value';
  16. let Password = 'some-string-value';
  17.  
  18. let authenticationDetails = new AuthenticationDetails({
  19. Username,
  20. Password
  21. });
  22.  
  23. let userPool = new CognitoUserPool({
  24. UserPoolId: USER_POOL_ID,
  25. ClientId: APP_CLIENT_ID
  26. });
  27.  
  28. let cognitoUser = new CognitoUser({
  29. Username,
  30. Pool: userPool
  31. });
  32.  
  33. let skateboards = {
  34. mfaRequired(codeDeliveryDetails) {
  35. let mfaCode = prompt('MFA code is required!');
  36.  
  37. cognitoUser.sendMFACode(mfaCode, mfaRequired);
  38. },
  39. newPasswordRequired(userAttributes, requiredAttributes) {
  40. delete userAttributes.email_verified; // it's returned but not valid to submit
  41.  
  42. let newPassword = prompt('A new password is required!');
  43.  
  44. cognitoUser.completeNewPasswordChallenge(newPassword, userAttributes, newPasswordRequired);
  45. }
  46. };
  47.  
  48. let updateAWSCreds = (jwtToken) => {
  49. AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  50. IdentityPoolId: IDENTITY_POOL_ID,
  51. Logins: {
  52. [POOL_KEY]: jwtToken
  53. }
  54. });
  55. };
  56.  
  57. let authenticateCognitoUser = async ({mfaRequired, newPasswordRequired} = skateboards) => {
  58. return new Promise((resolve, reject) => {
  59. cognitoUser.authenticateUser(authenticationDetails, {
  60. onSuccess(result) {
  61. let jwtToken = result.getIdToken().getJwtToken();
  62. updateAWSCreds(jwtToken);
  63. resolve();
  64. },
  65. onFailure(err) {
  66. reject(err);
  67. },
  68. mfaRequired,
  69. newPasswordRequired
  70. });
  71. });
  72. };
  73.  
  74. let doSomethingInS3ForExample = async () => {
  75. await authenticateCognitoUser();
  76.  
  77. // now do your stuff
  78. };
  79.  
  80. doSomethingInS3ForExample();
Add Comment
Please, Sign In to add comment