Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var TestApp = window.TestApp || {};
  2.  
  3. (function scopeWrapper($) {
  4.     var signinUrl = 'index.html';
  5.  
  6.     var poolData = {
  7.         UserPoolId: _config.cognito.userPoolId,
  8.         ClientId: _config.cognito.userPoolClientId
  9.     };
  10.  
  11.     var userPool;
  12.  
  13.     if (!(_config.cognito.userPoolId &&
  14.         _config.cognito.userPoolClientId &&
  15.         _config.cognito.region)) {
  16.         $('#noCognitoMessage').show();
  17.         return;
  18.     }
  19.  
  20.     userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  21.  
  22.     if (typeof AWSCognito !== 'undefined') {
  23.         AWSCognito.config.region = _config.cognito.region;
  24.     }
  25.  
  26.     TestApp.signOut = function signOut() {
  27.         userPool.getCurrentUser().signOut();
  28.     };
  29.  
  30.     TestApp.authToken = new Promise(function fetchCurrentAuthToken(resolve, reject) {
  31.         var cognitoUser = userPool.getCurrentUser();
  32.  
  33.         if (cognitoUser) {
  34.             cognitoUser.getSession(function sessionCallback(err, session) {
  35.                 if (err) {
  36.                     reject(err);
  37.                 } else if (!session.isValid()) {
  38.                     resolve(null);
  39.                 } else {
  40.                     resolve(session.getIdToken().getJwtToken());
  41.                 }
  42.             });
  43.         } else {
  44.             resolve(null);
  45.         }
  46.     });
  47.  
  48.     /*
  49.      * Cognito User Pool functions
  50.      */
  51.  
  52.     function register(email, name, password, onSuccess, onFailure) {
  53.  
  54.         var dataEmail = {
  55.             Name: 'email',
  56.             Value: email
  57.         };
  58.  
  59.         var fullName = {
  60.             Name: 'name',
  61.             Value: name
  62.         };
  63.  
  64.         var attributeName = new AmazonCognitoIdentity.CognitoUserAttribute(fullName);
  65.         var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
  66.  
  67.         var poolData = {
  68.             UserPoolId: 'us-east-1_lWqCuNtQd',   //_config.cognito.recruiterPoolId,
  69.             ClientId: '4rv0ibelu8sc3hi2dmjo05g5ku',  //_config.cognito.recruiterPoolClientId,
  70.         };
  71.  
  72.  
  73.         var userPool2 = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  74.  
  75.         userPool2.signUp(email, password, [attributeName, attributeEmail], null,
  76.             function signUpCallback(err, result) {
  77.                 if (!err) {
  78.                     onSuccess(result);
  79.                 } else {
  80.                     onFailure(err);
  81.                 }
  82.             }
  83.         );
  84.     }
  85.  
  86.     function update(name, oldpass, password, onSuccess, onFailure) {
  87.  
  88.         var cognitoUser = userPool.getCurrentUser();
  89.         if (cognitoUser != null) {
  90.             cognitoUser.getSession(function(err, session) {
  91.                 if (err) {
  92.                     alert(err);
  93.                     return;
  94.                 }
  95.                 console.log('session validity: ' + session.isValid());
  96.             });
  97.         }
  98.  
  99.         var fullName = {
  100.             Name: 'name',
  101.             Value: name
  102.         };
  103.  
  104.         var attributeName = new AmazonCognitoIdentity.CognitoUserAttribute(fullName);
  105.  
  106.         cognitoUser.updateAttributes([attributeName], function(err, result) {
  107.             if (!err) {
  108.                 onSuccess(result);
  109.             } else {
  110.                 onFailure(err);
  111.             }
  112.             console.log('call result: ' + result);
  113.         });
  114.  
  115.         cognitoUser.changePassword(oldpass, password , function(err, result) {
  116.             if (err) {
  117.                 alert(err);
  118.                 return;
  119.             }
  120.             console.log('call result: ' + result);
  121.         });
  122.  
  123.     }
  124.  
  125.     function signin(email, password, onSuccess, onFailure) {
  126.         var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
  127.             Username: email,
  128.             Password: password
  129.         });
  130.  
  131.         var cognitoUser = createCognitoUser(email);
  132.         cognitoUser.authenticateUser(authenticationDetails, {
  133.             onSuccess: onSuccess,
  134.             onFailure: onFailure
  135.         });
  136.     }
  137.  
  138.     function verify(email, code, onSuccess, onFailure) {
  139.         createCognitoUser(email).confirmRegistration(code, true, function confirmCallback(err, result) {
  140.             if (!err) {
  141.                 onSuccess(result);
  142.             } else {
  143.                 onFailure(err);
  144.             }
  145.         });
  146.     }
  147.  
  148.     function createCognitoUser(email) {
  149.         return new AmazonCognitoIdentity.CognitoUser({
  150.             Username: email,
  151.             Pool: userPool
  152.         });
  153.     }
  154.  
  155.     /*
  156.      *  Event Handlers
  157.      */
  158.  
  159.     $(function onDocReady() {
  160.         $('#registrationForm').submit(handleRegister);
  161.         $('#editForm').submit(handleEdition);
  162.         $('#signinForm').submit(handleSignin);
  163.         $('#verifyForm').submit(handleVerify);
  164.  
  165.     });
  166.    
  167.     function sendRequestToDB(id) {
  168.  
  169.         var toSend =
  170.             {
  171.                 "assignedTests": [],
  172.                 "candidateId": id
  173.             };
  174.  
  175.  
  176.         callRecruiterAwsLambda("POST", `candidatetests`, afterPostEmptyCandidate, toSend, false, userRoles.RECRUITER);
  177. }
  178.  
  179.     function afterPostEmptyCandidate(response) {
  180.         console.log(response);
  181.     }
  182.    
  183.     function getIdOfNewUser(desiredEmail) {
  184.         email = desiredEmail;
  185.         callRecruiterAwsLambda("GET", `candidate?email=${desiredEmail}`, afterGetUserList, '', false, userRoles.RECRUITER);
  186.     }
  187.  
  188.     let email;
  189.  
  190.     function afterGetUserList(response) {
  191.         let user = JSON.parse(response);
  192.         console.log(user);
  193.         sendRequestToDB(user.id);
  194.     }
  195.  
  196.     function handleSignin(event) {
  197.         var email = $('#emailInputSignin').val();
  198.         var password = $('#passwordInputSignin').val();
  199.         event.preventDefault();
  200.         signin(email, password,
  201.             function signinSuccess() {
  202.                 console.log('Successfully Logged In');
  203.                 window.location.href = 'MainView.html';
  204.             },
  205.             function signinError(err) {
  206.                 alert(err);
  207.             }
  208.         );
  209.     }
  210.  
  211.     function handleRegister(event) {
  212.         var email = $('#userEmail').val();
  213.         var userName = $('#userFullName').val();
  214.         var password = $('#pass').val();
  215.         var password2 = $('#passRepeat').val();
  216.  
  217.         var onSuccess = function registerSuccess(result) {
  218.             alert('Konto zostało poprawnie utworzone!\nSprawdź swoją skrzynkę pocztową!');
  219.             var cognitoUser = result.user;
  220.             console.log('user name is ' + cognitoUser.getUsername());
  221.             //getIdOfNewUser(email);
  222.             var confirmation = ('Registration successful. Please check your email inbox or spam folder for your verification code.');
  223.             if (confirmation) {
  224.            // window.location.href = 'MainView.html';
  225.             }
  226.         };
  227.         var onFailure = function registerFailure(err) {
  228.             alert(err);
  229.         };
  230.         event.preventDefault();
  231.  
  232.         if (password === password2) {
  233.             register(email, userName, password, onSuccess, onFailure);
  234.         } else {
  235.             alert('Podane hasła nie są takie same!');
  236.         }
  237.     }
  238.  
  239.     function handleEdition(event) {
  240.         var userName = $('#userEditedFullName').val();
  241.         var oldpassword = $('#oldPass').val();
  242.         var password = $('#editedPass').val();
  243.         var password2 = $('#editedPassRepeat').val();
  244.  
  245.         var onSuccess = function updateSuccess(result) {
  246.             alert('Konto zostało zaktualizowane!');
  247.             var confirmation = ('Aktualizacja zakończona pomyślnie.');
  248.             if (confirmation) {
  249.                 window.location.href = 'candidateTests.html';
  250.             }
  251.         };
  252.  
  253.         var onFailure = function registerFailure(err) {
  254.             alert(err);
  255.         };
  256.         event.preventDefault();
  257.  
  258.         if (password === password2) {
  259.             update(userName, oldpassword, password, onSuccess, onFailure);
  260.         } else {
  261.             alert('Podane hasła nie są takie same!');
  262.         }
  263.     }
  264.  
  265.     function handleVerify(event) {
  266.         var email = $('#emailInputVerify').val();
  267.         var code = $('#codeInputVerify').val();
  268.         event.preventDefault();
  269.         verify(email, code,
  270.             function verifySuccess(result) {
  271.                 console.log('call result: ' + result);
  272.                 console.log('Successfully verified');
  273.                 alert('Verification successful. You will now be redirected to the login page.');
  274.                 window.location.href = signinUrl;
  275.             },
  276.             function verifyError(err) {
  277.                 alert(err);
  278.             }
  279.         );
  280.     }
  281.  
  282.  
  283. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement