Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. /**
  2. * Signup function that uses AWS Cognito SDK. It wraps its API into a promise
  3. * to consume it easily from other parts of the app.
  4. */
  5. function logIn(username, password) {
  6. var authenticationDetails = new AuthenticationDetails({
  7. Username: username,
  8. Password: password,
  9. });
  10. User = new CognitoUser({
  11. Username: username,
  12. Pool: UserPool,
  13. });
  14. return new Promise(function(resolve, reject) {
  15. // Some of the AWS Cognito Identity functions take an object instead of
  16. // a callback to handle success and error cases. Wrapping them with
  17. // promises cleans up the inner workings of this library IMO.
  18. User.authenticateUser(authenticationDetails, {
  19. onSuccess: resolve,
  20. onFailure: reject,
  21. })
  22. })
  23. }
  24. /**
  25. * Function to handle the login form submit event.
  26. * It gets the values from the inputs, handles error and success cases, and
  27. * other UI related actions.
  28. */
  29. function handleSubmit(event) {
  30. event.preventDefault()
  31. var $inputs = $container.getElementsByTagName('input');
  32. // Blocks the submit button.
  33. startLoading()
  34. // Starts the login flow through Cognito.
  35. Cognito.logIn($inputs.email.value, $inputs.password.value)
  36. .then(function(result) {
  37. addAlert({
  38. type: 'success',
  39. message: 'Log in successful!'
  40. })
  41. console.log(result)
  42. })
  43. .catch(function(error) {
  44. // Unblocks the submit button
  45. stopLoading()
  46. // If the user needs to enter its confirmation code switch to the
  47. // confirmation form page.
  48. if (error.message === 'User is not confirmed.') {
  49. EventEmitter.emit('ConfirmForm:mount', {
  50. // We send the current user email just in case.
  51. email: $inputs.email.value,
  52. });
  53. EventEmitter.emit('LoginForm:unmount');
  54. return;
  55. }
  56. addAlert({
  57. type: 'error',
  58. message: error.message,
  59. })
  60. console.error(error)
  61. })
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement