Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {userPoolId, clientId } from './env.js'
  2. import {CognitoUserPool, CognitoUserAttribute} from 'amazon-cognito-identity-js'
  3. import {CognitoUser, AuthenticationDetails} from 'amazon-cognito-identity-js'
  4.  
  5. const userPoolData = {
  6.     UserPoolId : userPoolId,
  7.     ClientId : clientId
  8. }
  9. const userPool = new CognitoUserPool(userPoolData)
  10.  
  11.  
  12. const helloBtn = document.querySelector('#hello')
  13. helloBtn.addEventListener('click', () => {
  14.     alert("Hello Jakub")
  15. })
  16.  
  17. const bob = {
  18.     username: 'jkanclerz-123456',
  19.     password: '123qwe123',
  20.     email: 'gnr20280@cndps.com'
  21. }
  22.  
  23. const handleRegistration = (registerRequest) => {
  24.     userPool.signUp(
  25.         registerRequest.username,
  26.         registerRequest.password,
  27.         [
  28.             new CognitoUserAttribute({
  29.                 Name: 'name',
  30.                 Value: registerRequest.name
  31.             }),
  32.             new CognitoUserAttribute({
  33.                 Name: 'email',
  34.                 Value: registerRequest.email
  35.             })
  36.         ],
  37.         null,
  38.         (err, result) => {
  39.             if (err) {
  40.                 alert(err.message);
  41.                 return;
  42.             }
  43.            
  44.             console.log(`Hurayy user ${registerRequest.username} is registered`)
  45.         }
  46.     )
  47. }
  48. const registerBtn = document.querySelector('#testRegister')
  49. registerBtn.addEventListener('click', () => {
  50.     const registerRequest = {
  51.         username: bob.username,
  52.         password: bob.password,
  53.         name: 'Uncle bob',
  54.         email: bob.email,
  55.     }
  56.     handleRegistration(registerRequest)
  57. })
  58.  
  59. const handleLogin = (loginRequest) => {
  60.     const cognitoUser = new CognitoUser({
  61.         Username: loginRequest.Username,
  62.         Pool: userPool
  63.     })
  64.     const authDetails = new AuthenticationDetails(loginRequest)
  65.    
  66.     cognitoUser.authenticateUser(authDetails, {
  67.         onSuccess: (result) => {
  68.             console.log(result)
  69.             alert('Hurayy i am in');
  70.         },
  71.         onFailure: (err) => {
  72.             console.log(err);
  73.             alert("Something is not YES!!!!")
  74.         }
  75.     })
  76. }
  77.  
  78. const loginBtn = document.querySelector('#testLogin')
  79. loginBtn.addEventListener('click', () => {
  80.     const loginRequest = {
  81.         Username: bob.username,
  82.         Password: bob.password
  83.     }
  84.     handleLogin(loginRequest)
  85. })
  86.  
  87. const handleConfirm = (confirmRequest) => {
  88.     const cognitoUser = new CognitoUser({
  89.         Username: confirmRequest.Username,
  90.         Pool: userPool
  91.     })
  92.     cognitoUser.confirmRegistration(
  93.         confirmRequest.code,
  94.         true,
  95.         (err, result) => {
  96.         if (err) {
  97.             alert(err);
  98.             return;
  99.         }
  100.         alert('hurray confirmed');
  101.     });
  102. }
  103. const confirmBtn = document.querySelector('#testConfirm')
  104. confirmBtn.addEventListener('click', () => {
  105.     const confirmRequest = {
  106.         Username: bob.username,
  107.         code: '045088'
  108.     }
  109.     handleConfirm(confirmRequest);
  110. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement