Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <script src="dist/amazon-cognito-identity.min.js"></script>
  2. <script src="dist/aws-sdk.min.js"></script>
  3. <script>
  4. var authenticationData = {
  5. Username : 'test',
  6. Password : 'password',
  7. };
  8. var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
  9. var poolData = {
  10. UserPoolId : 'ap-southeast-1_xxxxxxx',
  11. ClientId : 'xxxxxxxxxxxxxxxxxxxxxxxxx'
  12. };
  13. var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  14. var userData = {
  15. Username : 'test',
  16. Pool : userPool
  17. };
  18. var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
  19. cognitoUser.authenticateUser(authenticationDetails, {
  20. onSuccess: function (result) {
  21. var accessToken = result.getAccessToken().getJwtToken();
  22.  
  23. /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer*/
  24. var idToken = result.idToken.jwtToken;
  25.  
  26. AWS.config.region = 'ap-southeast-1';
  27. AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  28. IdentityPoolId: 'ap-southeast-1:xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx',
  29. Logins: {
  30. 'cognito-idp.ap-southeast-1.amazonaws.com/ap-southeast-1_xxxxxx': idToken
  31. }
  32. });
  33.  
  34. AWS.config.credentials.get(function(err) {
  35. if (err) return console.error(err);
  36. else console.log(AWS.config.credentials);
  37.  
  38. var s3 = new AWS.S3({
  39. apiVersion: '2006-03-01',
  40. params: {Bucket: 'foo-test-bucket'}
  41. });
  42.  
  43. s3.listObjects({Delimiter: '/'}, function(err, data) {
  44. console.log(err, data)
  45. });
  46. });
  47. },
  48.  
  49. onFailure: function(err) {
  50. alert(err);
  51. },
  52.  
  53. });
  54. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement