token auth

This commit is contained in:
Andrew Gundersen 2022-02-09 08:31:58 -06:00
commit 1755c670d6
9 changed files with 41 additions and 32 deletions

View 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;

View 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;

View file

@ -1,16 +1,11 @@
const { app } = require("electron");
const env = process.env;
const PLATFORM_PORT = env.PLATFORM_PORT || 8760;
const PLATFORM_IP = env.PLATFORM_IP || 'http://127.0.0.1';
const BUSINESS_PORT = env.BUSINESS_PORT || 3000;
const BUSINESS_IP = env.BUSINESS_IP || 'http://127.0.0.1';
const DOMAIN = "crimata.com";
const PORT = 8760;
module.exports = {
PLATFORM_URL: `${PLATFORM_IP}:${PLATFORM_PORT}`,
BUSINESS_URL: `${BUSINESS_IP}:${BUSINESS_PORT}`,
BUSINESS_PREFIX: '/api',
configPath: app.getPath('userData'),
PLATFORM: app.isPackaged ? `http://app.${DOMAIN}` : `http://localhost:${PORT}`,
API: `https://${DOMAIN}/api`
}

View file

@ -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)

View 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";

View file

@ -32,8 +32,9 @@ 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;

View file

@ -125,7 +125,7 @@ html, body {
}
.login-input {
background-color: #B9B9B9;
background-color: #D3D3D3;
border: none;
outline: none;
padding: 16px;

View 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)