SHARE
TWEET

registerPage.js

a guest Oct 23rd, 2017 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const passwordHAS = require("password-hash-and-salt");
  4.  
  5. class Register {
  6.     constructor() {
  7.         this.usernameField = document.getElementById('username');
  8.         this.passwordFieldOne = document.getElementById('passwordOne');
  9.         this.passwordFieldTwo = document.getElementById('passwordTwo');
  10.         this.selectField = document.getElementById('userSelection');
  11.         this.errorField = document.getElementById('errorMessage');
  12.         this.loginButton = document.getElementById('loginButton');
  13.         this.backButton = document.getElementById('backButton');
  14.         this.connection = main.connection;
  15.         //this.connection.connect();
  16.         this.loginButton.addEventListener("click", (e) => {
  17.             e.preventDefault();
  18.             var validate = this.validateForm();
  19.             if (validate) {
  20.                 var duplicate = this.checkUsernameDuplicate();
  21.                 if (!duplicate) {
  22.                     this.hashAndSaltPassword();
  23.                 }
  24.             }
  25.         });
  26.  
  27.         this.backButton.addEventListener("click", () => {
  28.             location.href = "loginPage.pug";
  29.         });
  30.     }
  31.     validateForm() {
  32.         if (this.checkPasswordEquals() && !this.checkEmpty()) {
  33.             //console.log([this.usernameField.value, this.passwordFieldOne.value, this.passwordFieldTwo.value, this.selectField.value]);
  34.             this.errorField.innerHTML = '';
  35.             this.username = this.usernameField.value;
  36.             this.password = this.passwordFieldOne.value;
  37.             this.userType = this.selectField.value;
  38.             return true;
  39.         }
  40.         else if (this.checkEmpty()) {
  41.             this.errorField.innerHTML = "One or several fields are empty";
  42.             return false;
  43.         }
  44.         else if (!this.checkPasswordEquals()) {
  45.             this.errorField.innerHTML = "Passwords don't match";
  46.             return false;
  47.         }
  48.         else {
  49.             this.errorField.innerHTML = "Unknown error";
  50.             return false;
  51.         }
  52.     }
  53.     checkPasswordEquals() {
  54.         if (this.passwordFieldOne.value == this.passwordFieldTwo.value) {
  55.             return true;
  56.         }
  57.         else {
  58.             return false;
  59.         }
  60.     }
  61.     checkEmpty() {
  62.         if (this.usernameField.value == '' || this.passwordFieldOne.value == '' || this.passwordFieldTwo.value == '' || this.selectField.value == '') {
  63.             return true;
  64.         }
  65.         else
  66.             return false;
  67.     }
  68.     checkUsernameDuplicate() {
  69.         this.connection.query('SELECT * from USER', (err, result) => {
  70.             if (err) {
  71.                 console.log(err);
  72.                 return true;
  73.             }
  74.             for (let i = 0; i < result.length; i++) {
  75.                 if (this.username == result[i].username) {
  76.                     this.errorField.innerHTML = "Username is already taken";
  77.                     return true;
  78.                 }
  79.             }
  80.         });
  81.         return false;
  82.     }
  83.     hashAndSaltPassword() {
  84.         passwordHAS(this.password).hash((error, hash) => {
  85.             if (error) {
  86.                 throw new Error('Something went wrong');
  87.             }
  88.             this.insertIntoDatabase(hash);
  89.         });
  90.     }
  91.     insertIntoDatabase(hash) {
  92.         let query = "INSERT INTO `USER`(`username`, `usertype`, `password`) VALUES (?,?,?)";
  93.         let inputList = [this.username.toString(), this.userType.toString(), hash.toString()];
  94.         console.log("test");
  95.         console.log("INSERT INTO `USER`(`username`, `usertype`, `password`) VALUES " + `(${this.username.toString()},${this.userType.toString()},${hash.toString()})`);
  96.         this.connection.query(query, inputList, (error, results, fields) => {
  97.             if (error) {
  98.                 throw error;
  99.             }
  100.             else {
  101.                 this.errorField.style.color = '#FFF400';
  102.                 this.errorField.innerHTML = 'Account has been registered!';
  103.             }
  104.         });
  105.     }
  106. }
  107. window.onload = () => {
  108.     let register = new Register();
  109. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top