daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as mysql from 'mysql';
  2. import * as passwordHAS from 'password-hash-and-salt';
  3.  
  4. class Register {
  5.     usernameField = document.getElementById('username') as HTMLInputElement;
  6.     passwordFieldOne = document.getElementById('passwordOne') as HTMLInputElement;
  7.     passwordFieldTwo = document.getElementById('passwordTwo') as HTMLInputElement;
  8.     selectField = document.getElementById('userSelection') as HTMLSelectElement;
  9.     errorField = document.getElementById('errorMessage') as HTMLElement;
  10.     loginButton = document.getElementById('loginButton') as HTMLButtonElement;
  11.     connection: mysql.IConnection;
  12.     username: string;
  13.     password: string;
  14.     hashedPassword: string;
  15.     userType: string;
  16.  
  17.     constructor() {
  18.         this.connection = mysql.createConnection({
  19.             host: 'redacted',
  20.             user: 'redacted',
  21.             password: 'redacted',
  22.             database: 'redacted',
  23.         });
  24.         this.connection.connect();
  25.         this.loginButton.addEventListener("click", (e: any) => {
  26.             e.preventDefault();
  27.             var validate = this.validateForm();
  28.             if(validate) {
  29.                 var duplicate = this.checkUsernameDuplicate();
  30.                 if(!duplicate) {
  31.                     this.hashAndSaltPassword();
  32.                     console.log(this.hashedPassword);
  33.                     if(this.hashedPassword != undefined) {
  34.                         this.insertIntoDatabase();
  35.                     }
  36.                 }
  37.             }
  38.         });
  39.     }
  40.  
  41.     validateForm(): boolean {
  42.         if(this.checkPasswordEquals() && !this.checkEmpty()) {
  43.             console.log([this.usernameField.value, this.passwordFieldOne.value, this.passwordFieldTwo.value, this.selectField.value]);
  44.             this.errorField.innerHTML = '';
  45.             this.username = this.usernameField.value;
  46.             this.password = this.passwordFieldOne.value;
  47.             this.userType = this.selectField.value;
  48.             return true;
  49.         } else if (this.checkEmpty()) {
  50.             this.errorField.innerHTML = "One or several fields are empty";
  51.             return false;
  52.         }
  53.         else if(!this.checkPasswordEquals()) {
  54.             this.errorField.innerHTML = "Passwords don't match";
  55.             return false;
  56.         } else {
  57.             this.errorField.innerHTML = "Unknown error";
  58.             return false;
  59.         }
  60.     }
  61.  
  62.     checkPasswordEquals(): boolean {
  63.         if(this.passwordFieldOne.value == this.passwordFieldTwo.value) {
  64.             return true;
  65.         } else {
  66.             return false;
  67.         }
  68.     }
  69.  
  70.     checkEmpty():boolean {
  71.         if(this.usernameField.value == '' || this.passwordFieldOne.value == '' || this.passwordFieldTwo.value == '' || this.selectField.value == '') {
  72.             return true;
  73.         } else return false;
  74.     }
  75.  
  76.     checkUsernameDuplicate():boolean {
  77.         this.connection.query('SELECT * from USER', (err, result) => {
  78.             if(err) {
  79.                 console.log(err);
  80.                 return true;
  81.             }
  82.             for(let i = 0; i < result.length; i++) {
  83.                 if(this.username == result[i].username) { //checks if username already exists in database
  84.                     this.errorField.innerHTML = "Username is already taken";
  85.                     return true;
  86.                 }
  87.             }
  88.         });
  89.         return false;
  90.     }
  91.  
  92.     insertIntoDatabase():void {
  93.         this.connection.query(`INSERT INTO USER VALUES ('${this.username}', 'NULL', 'NULL', '${this.userType}', '${this.hashedPassword}')`, (error, results, fields) => {
  94.             if(error) {
  95.                 throw error;
  96.             } else {
  97.                 this.errorField.style.color = '#FFF400';
  98.                 this.errorField.innerHTML = 'Account has been registered!';
  99.             }
  100.         });
  101.     }
  102.  
  103.     hashAndSaltPassword():void {
  104.         passwordHAS(this.password).hash((error, hash) => {
  105.             if(error) {
  106.                 throw new Error('Something went wrong');
  107.             }
  108.             this.hashedPassword = hash;
  109.             console.log(this.hashedPassword);
  110.         });
  111.     }
  112.  
  113. }
  114.  
  115. window.onload = () => {
  116.     let register = new Register();
  117. }
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