Advertisement
Guest User

Untitled

a guest
May 14th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. const CognitoIdentityServiceProvider = require("amazon-cognito-identity-js");
  2. // Required in order to use the cognito js library to work.
  3. global.fetch = require("node-fetch");
  4.  
  5. /**
  6. * Authenticate a cognito user and return its authentication token. Use the auth token in the authorization header
  7. * @param callback Callback function with error as first param and the actual user token in the second param.
  8. */
  9.  
  10. function authenticateUser(callback) {
  11. console.info("Authenticating user");
  12. const authenticationData = {
  13. Username: userName,
  14. Password: password
  15. };
  16.  
  17. const authenticationDetails = new CognitoIdentityServiceProvider.AuthenticationDetails(
  18. authenticationData
  19. );
  20. const poolData = {
  21. UserPoolId: userPoolId, // Your user pool id here
  22. ClientId: clientId // Your client id here
  23. };
  24.  
  25. const userPool = new CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  26. const userData = {
  27. Username: userName,
  28. Pool: userPool
  29. };
  30.  
  31. const cognitoUser = new CognitoIdentityServiceProvider.CognitoUser(userData);
  32. cognitoUser.authenticateUser(authenticationDetails, {
  33. onSuccess: function(result) {
  34. const token = result.getIdToken().getJwtToken();
  35. console.info(`User token: ${token}`);
  36. callback(null, token);
  37. },
  38. onFailure: function(err) {
  39. callback(err);
  40. }
  41. });
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement