Browse Source

- Add login

Parad0x 7 years ago
parent
commit
1acf789400
2 changed files with 26 additions and 1 deletions
  1. 23 0
      src/connectionHandler.js
  2. 3 1
      src/db/App.js

+ 23 - 0
src/connectionHandler.js

@@ -12,6 +12,7 @@ const Connections = {};
 // List of operations should be executed
 // List of operations should be executed
 const secureOp = [
 const secureOp = [
   "register",
   "register",
+  "login",
   "ping",
   "ping",
   "close"];
   "close"];
 
 
@@ -20,6 +21,7 @@ class Connection {
   socket: Socket;
   socket: Socket;
   buffer: string;
   buffer: string;
   queue: Queue<string>;
   queue: Queue<string>;
+  session: any;
 
 
   constructor(id: number, socket: Socket) {
   constructor(id: number, socket: Socket) {
     console.log("New connection", id);
     console.log("New connection", id);
@@ -73,6 +75,27 @@ class Connection {
     this.write(`${instance.id}&${instance.auth};`);
     this.write(`${instance.id}&${instance.auth};`);
   }
   }
 
 
+  async login() {
+    if (typeof this.session !== "undefined") {
+      this.write("error:session is logged;");
+      return;
+    }
+    const auth = await this.queue.remove();
+    const user = await db.App.findOne({
+      where: {
+        auth
+      }
+    });
+    if (!user) {
+      this.write("error:auth unmatched;");
+      return;
+    }
+    this.session = {
+      user
+    };
+    this.write("info:loggedin;");
+  }
+
   ping() {
   ping() {
     this.write("pong;");
     this.write("pong;");
   }
   }

+ 3 - 1
src/db/App.js

@@ -10,7 +10,9 @@ const App = sequelize.define("app", {
   },
   },
   auth: {
   auth: {
     type: Sequelize.STRING,
     type: Sequelize.STRING,
-    defaultValue: ""
+    defaultValue: "",
+    unique: true,
+    primaryKey: true
   },
   },
   activated: {
   activated: {
     type: Sequelize.BOOLEAN,
     type: Sequelize.BOOLEAN,