Sfoglia il codice sorgente

Create the User class for the frontend.

Lee Morgan 1 anno fa
parent
commit
77efb70a7a
2 ha cambiato i file con 37 aggiunte e 10 eliminazioni
  1. 31 0
      src/views/js/data/User.js
  2. 6 10
      src/views/js/pages/Register.js

+ 31 - 0
src/views/js/data/User.js

@@ -0,0 +1,31 @@
+import EncryptionHandler from "../EncryptionHandler.js";
+
+export default class User{
+    constructor(id, name, email, salt, accounts = []){
+        this._id = id;
+        this._name = name;
+        this._email = email;
+        this._salt = salt;
+        this._accounts = accounts;
+    }
+
+    static async create(name, email, password){
+        const passwordSalt = EncryptionHandler.generateSalt();
+
+        return {
+            name: name,
+            email: email,
+            password_hash: await EncryptionHandler.hashPassword(password, salt),
+            password_salt: passwordSalt,
+            encryption_salt: EncryptionHandler.generateSalt()
+        };
+    }
+
+    get id(){
+        return this._id;
+    }
+
+    get salt(){
+        return this._salt;
+    }
+}

+ 6 - 10
src/views/js/pages/Register.js

@@ -1,6 +1,6 @@
 import Page from "./Page.js";
 import Elem from "../Elem.js";
-import EncryptionHandler from "../EncryptionHandler.js";
+import User from "../data/User.js";
 
 export default class Register extends Page{
     constructor(){
@@ -17,26 +17,22 @@ export default class Register extends Page{
         const password = this.container.querySelector(".password").value;
         const confirmPassword = this.container.querySelector(".confirmPassword").value;
 
-        return;
+        const user = User.create(name, email, password);
+        console.log(user);
+
         fetch("/api/user", {
             method: "POST",
             headers: {
                 "Content-Type": "application/json"
             },
-            body: JSON.stringify({
-                name: name,
-                email: email,
-                password_hash: passwordHash,
-                password_salt: passwordSalt,
-                encryption_salt: encryption.salt,
-            })
+            body: JSON.stringify(user)
         })
             .then(r=>r.json())
             .then((response)=>{
                 if(response.error){
                     console.log(response.error);
                 }else{
-                    changePage("login");
+                    console.log(response);
                 }
             })
             .catch((err)=>{