]> Repos - mime-chat/commitdiff
token auth
authorAndrew Gundersen <gundersena@xavier.edu>
Wed, 9 Feb 2022 14:31:58 +0000 (08:31 -0600)
committerAndrew Gundersen <gundersena@xavier.edu>
Wed, 9 Feb 2022 14:31:58 +0000 (08:31 -0600)
src/account.js
src/api.js
src/config.js
src/io.js
src/main.js
src/render/components/app.js
src/render/components/control/drop.js
src/render/main.css
src/session.js

index 29852f77e84058972c51f796322ab2e193a2eb57..ee8324570e96aa96ee840cfef9a6c5568ad57ea7 100644 (file)
@@ -2,21 +2,22 @@ const { ipcMain } = require("electron");
 
 const store = require("./utils/store");
 const { postLogin } = require("./api");
-const { ipcEmit } = require("./utils/emitter");
 const { launchSession, endSession } = require("./session");
+const { backgroundMitt, ipcEmit } = require("./utils/emitter");
 
-store.set("account", "smith12@gmail.com");
-store.set("account", "adgundersen@gmail.com");
+store.set("account", "0000");
 
 async function login(_e, email, password) 
 {
     const res = await postLogin(email, password);
     if (res) 
     {
-        const account = res.crimataId;
+        const account = res.token;
         store.set("account", account);
         ipcEmit("set-account", account);
+
         launchSession(account);
+
         return true;
     } 
     else
@@ -25,10 +26,10 @@ async function login(_e, email, password)
     }
 };
 
-function logout()
+function logout(reason)
 {
     store.delete("account");
-    ipcEmit("set-account", false);
+    ipcEmit("set-account", false, reason);
     endSession();
 };
 
@@ -41,4 +42,6 @@ function initAccount()
     if (account) launchSession(account);
 }
 
+backgroundMitt.on("logout", (reason) => logout(reason));
+
 exports.initAccount = initAccount;
index 89d617e2cf335a1f661f83fe1f23c69876b6d838..7a8db278a4411c39c1dddef3a60b0aefaeba31f5 100644 (file)
@@ -2,12 +2,10 @@ const axios = require('axios').default;
 
 const config = require("./config");
 
-const baseUrl = config.BUSINESS_URL + config.BUSINESS_PREFIX;
-
 async function postLogin(email, password)
 {
     try {
-        const res = await axios.post(baseUrl + "/account/login", {
+        const res = await axios.post(config.API + "/login", {
             email: email, password: password
         });
         return res.data;
index adf5e22ab2457e66ad5fa76b3fbc3c1909e52e0f..2af5e9c4c525055f8d9afc5381803e526a6bc79a 100644 (file)
@@ -1,16 +1,11 @@
 const { app } = require("electron");
 
-const env = process.env;
+const DOMAIN = "crimata.com";
+const PORT = 8760;
 
-const PLATFORM_PORT = env.PLATFORM_PORT || 8760;
-const PLATFORM_IP = env.PLATFORM_IP || 'http://127.0.0.1';
+module.exports = {
 
-const BUSINESS_PORT = env.BUSINESS_PORT || 3000;
-const BUSINESS_IP = env.BUSINESS_IP || 'http://127.0.0.1';
+PLATFORM: app.isPackaged ? `http://app.${DOMAIN}` : `http://localhost:${PORT}`,
+API: `https://${DOMAIN}/api`
 
-module.exports = {
-    PLATFORM_URL: `${PLATFORM_IP}:${PLATFORM_PORT}`,
-    BUSINESS_URL: `${BUSINESS_IP}:${BUSINESS_PORT}`,
-    BUSINESS_PREFIX: '/api',
-    configPath: app.getPath('userData'),
-}
\ No newline at end of file
+}
index 435377e57af0a16c6aac88ef2179e84d26beaeb9..701fbe4a0ab672622de2cb2624581ed16935f029 100644 (file)
--- a/src/io.js
+++ b/src/io.js
@@ -14,7 +14,7 @@ function connectToPlatform(account)
 {
     if (pingId) clearInterval(pingId);
 
-    connection = new WebSocket(`${config.PLATFORM_URL}/${account}`)
+    connection = new WebSocket(`${config.PLATFORM}/${account}`)
 
     .on("open", () => pingId = setInterval(() => connection.ping(null, true), 1000))
 
@@ -24,11 +24,22 @@ function connectToPlatform(account)
 
     .on("message", (payload) => backgroundMitt.emit("message", JSON.parse(payload)))
 
-    .on("close", (code, reason) => {
+    .on("close", (_code, reason) => {
+
         setConnectionStatus(1);
-        if (reason !== "logout") {
-            reconnectId = setTimeout(() => connectToPlatform(account), 500);
+
+        if (reason)
+        {
+            if (reason == "unauthorized")
+            {
+                backgroundMitt.emit("logout", reason);
+            }
+
+            return;
         }
+
+        reconnectId = setTimeout(() => connectToPlatform(account), 500);
+
     });
 }
 
@@ -42,7 +53,7 @@ function sendMessage(message)
 function disconnectFromPlatform()
 {
    clearTimeout(reconnectId);
-   connection.close(1000, "logout");
+   if (connection.open) connection.close(1000, "logout");
 }
 
 function setConnectionStatus(status)
index b5caad7bd93f2bf596af04a3c0b6cfcd6013a11f..841579ecdcb781a841b57c61410a61959b8861a5 100644 (file)
@@ -8,8 +8,6 @@ const { createWin } = require("./window");
 const { initAccount } = require("./account"); 
 const { terminateAudio } = require("./audio");
 
-app.commandLine.appendSwitch("enable-transparent-visuals");
-
 // Assert a light theme
 nativeTheme.themeSource = "light";
 
index 37d5a1b63ae698ab1f00c198e82957747f19afe7..cfd831bdcb1c3e6dd40f6df47442c8313e5813d9 100644 (file)
@@ -32,12 +32,13 @@ const App =
         <Splash v-else />`
 }
 
-window.mainApi.on("set-account", (account_) => {
-    account.value = account_;
+window.mainApi.on("set-account", (value, reason) => {
+    if (reason) alert(reason);
+    account.value = value;
 });
 
 export default App;
 export { account };
 
 // Is called when window is about to close or reload
-window.onbeforeunload = () => console.log("beforeunload");
\ No newline at end of file
+window.onbeforeunload = () => console.log("beforeunload");
index f837c395fbc0ba4133a69e46cbcf88242b9202e6..ac9c790a928cf379ecdd117bebc56578d909508a 100644 (file)
@@ -21,4 +21,4 @@ const onDrop = async (e) => {
     });
 }
 
-export default onDrop;
\ No newline at end of file
+export default onDrop;
index f3d6a95fa4c0d79178e657b95fb3d2f7a30d2e64..9c2f640bb4840a713e359a1841fd90833daf4882 100644 (file)
@@ -125,7 +125,7 @@ html, body {
 }
 
 .login-input {
-  background-color: #B9B9B9;
+  background-color: #D3D3D3;
   border: none;
   outline: none;
   padding: 16px;
index 67f3c8e75fdd72763ea4813c0b765e2d22d0f401..0ef665ed3fe11bfe41361454218570d27dc5082a 100644 (file)
@@ -5,6 +5,7 @@ const { backgroundMitt, ipcEmit } = require("./utils/emitter");
 const { connectToPlatform, disconnectFromPlatform } = require("./io");
 const { initAudio, terminateAudio, playback, streamState } = require("./audio");
 
+const util = require('util');
 let ui;
 
 const messageQueue = [];
@@ -26,6 +27,8 @@ function handleMessageQueue()
     {
         const update = messageQueue.shift();
 
+        console.log(util.inspect(update, false, null, true /* enable colors */));
+
         if (update)
         {
             if (update.person)