Advertisement
Guest User

Untitled

a guest
May 12th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. ```
  2. const login = () => {
  3.  
  4. let Username = 'reacttest';
  5. let Password = 'reacttest';
  6.  
  7. let authenticationDetails = new AuthenticationDetails({
  8. Username,
  9. Password
  10. });
  11.  
  12. const { UserPoolId, ClientId, region } = config;
  13. var poolData = { UserPoolId, ClientId, region };
  14. var userPool = new CognitoUserPool(poolData);
  15. var userData = {
  16. Username : 'reacttest',
  17. Pool : userPool
  18. };
  19. var cognitoUser = new CognitoUser(userData);
  20.  
  21. let sessionData;
  22. if (cognitoUser != null) {
  23. cognitoUser.getSession(function(err, session) {
  24. if (err) {
  25. alert(err);
  26. return;
  27. };
  28. sessionData = session;
  29. console.log('sessionData', sessionData);
  30. console.log('session validity: ' + session.isValid());
  31. });
  32. }
  33.  
  34. if (sessionData)
  35. console.log('idToken =>>>', sessionData.idToken.getJwtToken());
  36.  
  37. cognitoUser.authenticateUser(authenticationDetails, {
  38.  
  39. onSuccess: function (result) {
  40.  
  41. // get identity token
  42. // identityPoolId and poolData coming from config.js
  43. var params = {
  44. IdentityPoolId: config.IdentityPoolId,
  45. Logins: {
  46. [`cognito-idp.${config.region}.amazonaws.com/${config.UserPoolId}`]:
  47. result.getIdToken().getJwtToken()
  48. }
  49. };
  50.  
  51. // get credentials
  52. AWS.config.credentials = new AWS.CognitoIdentityCredentials(params, function(err, data) {
  53. if (err) console.log(err, err.stack);
  54. else console.log('AWS.config.credentials', data);
  55. });
  56.  
  57. AWS.config.credentials.get(function(err){
  58. if (err) {
  59. alert(err);
  60. } else {
  61. var accessKeyId = AWS.config.credentials.accessKeyId;
  62. var secretAccessKey = AWS.config.credentials.secretAccessKey;
  63. var sessionToken = AWS.config.credentials.sessionToken;
  64.  
  65. console.log('accessKeyId =>>>', accessKeyId);
  66. console.log('secretKey =>>>', secretAccessKey);
  67. console.log('sessionToken =>>>', sessionToken);
  68. console.log('region =>>>', poolData.region);
  69.  
  70. // eslint-disable-next-line
  71. var apigClient = apigClientFactory.newClient({
  72. accessKey: accessKeyId,
  73. secretKey: secretAccessKey,
  74. sessionToken: sessionToken,
  75. region: poolData.region
  76. });
  77.  
  78. // here we could add an api call
  79.  
  80. var params = {};
  81. var additionalParams = {
  82. headers: {
  83. // Authorization: sessionData.idToken
  84. Authorization: sessionData.idToken.getJwtToken()
  85. },
  86. queryParams: {}
  87. };
  88. var body = {};
  89.  
  90. apigClient.identityGet(params, body, additionalParams)
  91. .then(function(result){
  92. if (err) {
  93. alert(err);
  94. } else {
  95. console.log(JSON.stringify(result.data));
  96. }
  97. }).catch( function(result){
  98. //This is where you would put an error callback
  99. if (err) {
  100. alert(err);
  101. }
  102. });
  103. }
  104. });
  105.  
  106. },
  107. onFailure: function(err) {
  108. alert(err);
  109. },
  110. });
  111. };
  112. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement