From: Andrew Gundersen Date: Wed, 9 Feb 2022 14:31:58 +0000 (-0600) Subject: token auth X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=1755c670d6b5656b788936266a243c3c50603d8b;p=mime-chat token auth --- diff --git a/src/account.js b/src/account.js index 29852f7..ee83245 100644 --- a/src/account.js +++ b/src/account.js @@ -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; diff --git a/src/api.js b/src/api.js index 89d617e..7a8db27 100644 --- a/src/api.js +++ b/src/api.js @@ -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; diff --git a/src/config.js b/src/config.js index adf5e22..2af5e9c 100644 --- a/src/config.js +++ b/src/config.js @@ -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 +} diff --git a/src/io.js b/src/io.js index 435377e..701fbe4 100644 --- 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) diff --git a/src/main.js b/src/main.js index b5caad7..841579e 100644 --- a/src/main.js +++ b/src/main.js @@ -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"; diff --git a/src/render/components/app.js b/src/render/components/app.js index 37d5a1b..cfd831b 100644 --- a/src/render/components/app.js +++ b/src/render/components/app.js @@ -32,12 +32,13 @@ const App = ` } -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"); diff --git a/src/render/components/control/drop.js b/src/render/components/control/drop.js index f837c39..ac9c790 100644 --- a/src/render/components/control/drop.js +++ b/src/render/components/control/drop.js @@ -21,4 +21,4 @@ const onDrop = async (e) => { }); } -export default onDrop; \ No newline at end of file +export default onDrop; diff --git a/src/render/main.css b/src/render/main.css index f3d6a95..9c2f640 100644 --- a/src/render/main.css +++ b/src/render/main.css @@ -125,7 +125,7 @@ html, body { } .login-input { - background-color: #B9B9B9; + background-color: #D3D3D3; border: none; outline: none; padding: 16px; diff --git a/src/session.js b/src/session.js index 67f3c8e..0ef665e 100644 --- a/src/session.js +++ b/src/session.js @@ -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)