Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
183
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} from 'amazon-cognito-identity-js'
  3. import {CognitoUserAttribute} from 'amazon-cognito-identity-js'
  4. import {CognitoUser} from 'amazon-cognito-identity-js'
  5. import {AuthenticationDetails} from 'amazon-cognito-identity-js'
  6.  
  7. var userPoolData = {
  8.     UserPoolId : userPoolId,
  9.     ClientId   : clientId
  10. }
  11.    
  12. const userPool = new CognitoUserPool(userPoolData)
  13.  
  14. const helloBtn = document.querySelector('#hello')
  15.  
  16. helloBtn.addEventListener('click', ()=> {
  17.     alert("No siema ziomeczki")
  18. })
  19.  
  20. const bob = {
  21.     userName: 'lubiePlackiDobre',
  22.     password: '123qwe123',
  23.     email: 'wtt59574@cndps.com'
  24. }
  25.  
  26. const handleRegistration = (rRequest) => {
  27.     userPool.signUp(
  28.         rRequest.username,
  29.         rRequest.password,
  30.         [
  31.             new CognitoUserAttribute({
  32.                 Name: 'name',
  33.                 Value: rRequest.username,
  34.             }),
  35.             new CognitoUserAttribute({
  36.                 Name: 'email',
  37.                 Value: rRequest.email,
  38.             })
  39.         ],
  40.         null,
  41.         (err, result) => {
  42.             if(err){
  43.                 alert(err.message);
  44.                 return;
  45.             }
  46.            
  47.             console.log('yaay ' + rRequest.username +  ' is registered');
  48.         }
  49.     )
  50. }
  51.  
  52.  
  53. const registerBtn = document.querySelector('#testRegister')
  54.  
  55. registerBtn.addEventListener('click', ()=> {
  56.     const rRequest = {
  57.         username: bob.userName,
  58.         password: bob.password,
  59.         email: bob.email
  60.     }
  61.     handleRegistration(rRequest)
  62. })
  63.  
  64. const handleLogin = (loginRequest) => {
  65.    
  66.     const cognitoUser = new CognitoUser({
  67.         Username: loginRequest.username,
  68.         Pool: userPool,
  69.     });
  70.    
  71.     const authDetails = new AuthenticationDetails({
  72.         Username: loginRequest.username,
  73.         Password: loginRequest.password
  74.        
  75.     });
  76.    
  77.     cognitoUser.authenticateUser(authDetails, {
  78.         onSuccess: (result) => {
  79.             console.log(result);
  80.             alert("Yaaay, I am in!");
  81.         },
  82.        
  83.         onFailure: (err) => {
  84.             console.log(err);
  85.             alert("Omg, does not wooork :(");
  86.         }
  87.        
  88.     })
  89. }
  90.  
  91.  
  92. const loginBtn = document.querySelector('#testLogin')
  93. loginBtn.addEventListener('click', ()=>{
  94.     const loginRequest = {
  95.         username : bob.userName,
  96.         password : bob.password
  97.     }
  98.    
  99.     handleLogin(loginRequest)
  100. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement