SHARE
TWEET

Untitled

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