From: Enrique Hernandez Date: Thu, 11 Feb 2021 04:34:07 +0000 (-0600) Subject: fix post window close auth bug X-Git-Tag: v0.9~90 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=6a19a9685561afeb909be5c22a7fb9321bf8628e;p=mime-chat fix post window close auth bug --- diff --git a/.eslintrc.js b/.eslintrc.js index 66a98d1..eaf98f7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,7 +13,12 @@ module.exports = { }, rules: { "no-console": process.env.NODE_ENV === "production" ? "warn" : "off", - "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off" + "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off", + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": [ + "error", + { "argsIgnorePattern": "^_" } + ] }, overrides: [ { @@ -23,7 +28,7 @@ module.exports = { "*.js" ], rules: { - "@typescript-eslint/no-var-requires": "off" + "@typescript-eslint/no-var-requires": "off", }, env: { jest: true diff --git a/src/background/createWindow.ts b/src/background/createWindow.ts index 8b280c3..9ff1773 100644 --- a/src/background/createWindow.ts +++ b/src/background/createWindow.ts @@ -1,7 +1,9 @@ "use strict"; -import { BrowserWindow } from "electron"; +import { BrowserWindow, ipcMain } from "electron"; import { createProtocol } from "vue-cli-plugin-electron-builder/lib"; +import { windowEmitter } from './windowEmitter'; +import { sendMessage } from './session'; import * as path from "path"; interface WindowSettings { @@ -10,8 +12,17 @@ interface WindowSettings { resizable: boolean; } +const handleMessage = (event: any, arg: any) => { + sendMessage(arg); +} + +const windowMount = (): void => { + windowEmitter.emit('window-active', true); + ipcMain.on('send-message', handleMessage); +} + export const createWindow = async (options: WindowSettings): Promise => { - return new Promise((resolve, reject) => { + return new Promise((resolve, _reject) => { const win: BrowserWindow = new BrowserWindow({ width: options.width, height: options.height, @@ -35,8 +46,10 @@ export const createWindow = async (options: WindowSettings): Promise { + windowMount(); resolve(win); }); - }) -} + + }); +}; diff --git a/src/background/run.ts b/src/background/run.ts index e231575..42f8069 100644 --- a/src/background/run.ts +++ b/src/background/run.ts @@ -1,58 +1,50 @@ +"use strict"; + import { createWindow } from './createWindow'; import { ipcMain, BrowserWindow } from "electron"; -import { initSession, sendMessage } from './session'; +import { initSession } from './session'; import { windowEmitter } from './windowEmitter'; -const portAudio = require('naudiodon'); +// const portAudio = require('naudiodon'); -interface ipcRendererPayload { +interface IpcRendererPayload { endpoint: string; message: any; } -let activeSession = false; let win: BrowserWindow | null; +let activeSession = false; -windowEmitter.on('ipc-renderer', (payload: ipcRendererPayload) => { +windowEmitter.on('ipc-renderer', (payload: IpcRendererPayload) => { if (win) win.webContents.send(payload.endpoint, { message: payload.message }); }); +const windowDismount = (): void => { + win = null; + windowEmitter.emit('window-active', false); + ipcMain.removeAllListeners('send-message'); +} + /* * The main function will be run after electron app is ready. */ export async function main() { // create main window. - if (!win) { - win = await createWindow({ width: 350, height: 525, resizable: false }); - } - + if (!win) win = await createWindow({ + width: 350, + height: 525, + resizable: false + }); // Instantiate socket session with crimata-platorm. - if (!activeSession) { - initSession(); - } - activeSession = true; - - const handleMessage = (event: any, arg: any) => { - sendMessage(arg); - } + if (!activeSession) initSession(); - const windowMount = (): void => { - windowEmitter.emit('window-active', true); - ipcMain.on('send-message', handleMessage); - } - - const windowDismount = (): void => { - win = null; - windowEmitter.emit('window-active', false); - ipcMain.removeAllListeners('send-message'); - } + activeSession = true; - // Handle window mount and dismount. - win.webContents.on('did-finish-load', windowMount); + // Handle window close. win.on("closed", windowDismount); // TODO: FIX recorder restart bug diff --git a/src/background/session.ts b/src/background/session.ts index d2d1883..bee5f43 100644 --- a/src/background/session.ts +++ b/src/background/session.ts @@ -1,15 +1,10 @@ +"use strict"; + import WebSocket from 'ws'; import { windowEmitter } from './windowEmitter'; import { ipcMain } from "electron"; -const EventEmitter = require('events'); - -class SocketEmitter extends EventEmitter {} - -export const socketEmitter = new SocketEmitter(); - interface RenderMessage { - type: 'render'; content: string; context: string; subContext: string; @@ -33,24 +28,21 @@ let auth = false; const receiveMessage = (message: string): void => { while(!auth) { - console.log('window emitter @ session.ts recieve') windowEmitter.emit('auth-res', message); return; } const parsed: RenderMessage = JSON.parse(message); - if (parsed.type === "render") { - windowEmitter.emit('ipc-renderer', { - endpoint: 'render-message', - message: parsed - }); - } -} -const authUser = async (event: any, payload: string | UserCreds | null): Promise => { + windowEmitter.emit('ipc-renderer', { + endpoint: 'render-message', + message: parsed + }); +}; + +const authSession = async (_event, payload: string | UserCreds | null): Promise => { return new Promise((resolve, reject) => { console.log('authenticating...'); - console.log('window emitter @ session.ts auth') windowEmitter.on('auth-res', (res: string) => { if (res === 'locked') { reject(res); @@ -69,6 +61,14 @@ const authUser = async (event: any, payload: string | UserCreds | null): Promise }); }; +export const sendMessage = (content: string | Buffer) => { + if (content instanceof Buffer) { + socket.send(content); + } else if (content){ + socket.send(JSON.stringify(content)); + } +} + export const initSession = () => { if (socket) { socket.removeAllListeners(); @@ -84,7 +84,7 @@ export const initSession = () => { // handle renderer auth-token event ipcMain.removeHandler('auth-user'); // avoid setting duplicate handlers - ipcMain.handle('auth-user', authUser); + ipcMain.handle('auth-user', authSession); socket.on('open', () => { console.log('Success! Connected to Crimata.'); @@ -98,7 +98,7 @@ export const initSession = () => { }); - socket.on('error', (e) => { + socket.on('error', (_e) => { console.log('ERROR: Failed to connect.'); socket.removeAllListeners(); socket.close(); @@ -109,20 +109,13 @@ export const initSession = () => { } }, reconnectTimeout); - }) + }); socket.on('close', () => { console.log('Connection droped. Restarting.') initSession(); - }) + }); socket.on("message", receiveMessage); -} +}; -export const sendMessage = (content: string | Buffer) => { - if (content instanceof Buffer) { - socket.send(content); - } else if (content){ - socket.send(JSON.stringify(content)); - } -} diff --git a/src/modules/auth.ts b/src/modules/auth.ts index ae908f1..73edaf3 100644 --- a/src/modules/auth.ts +++ b/src/modules/auth.ts @@ -1,13 +1,13 @@ -import { reactive, watch, toRefs } from 'vue'; +import { reactive, toRefs } from 'vue'; import { useIpc } from './ipc'; interface AuthState { - access_token?: string | null; + accessToken?: string | null; error?: Error; } const state = reactive({ - access_token: undefined, + accessToken: undefined, error: undefined, }); @@ -15,20 +15,28 @@ const AUTH_KEY = 'crimata_token'; const token = window.localStorage.getItem(AUTH_KEY); -// authenticate on socket connect -window.ipcRenderer.on("fetch-token", async (event, payload: null) => { - const { data, invoke } = useIpc('auth-user'); +const authToken = async () => { + const { data, invoke } = useIpc('auth-user'); try { await invoke(token); - state.access_token = data.value; + state.accessToken = data.value; } catch(e) { state.error = e; console.log('ERROR: failed authentication'); window.localStorage.removeItem(AUTH_KEY); } +} + +// authenticate on socket connect +window.ipcRenderer.on("fetch-token", async (_event, _arg) => { + authToken(); }); +if (token) { + authToken(); +} + export const useAuth = () => { const setToken = (token: string, remember: boolean) => { if (remember) { @@ -36,18 +44,18 @@ export const useAuth = () => { window.localStorage.setItem(AUTH_KEY, token); } - state.access_token = token; + state.accessToken = token; state.error = undefined; } const logout = (): Promise => { window.localStorage.removeItem(AUTH_KEY); - return Promise.resolve(state.access_token = undefined); + return Promise.resolve(state.accessToken = undefined); } return { setToken, logout, - ...toRefs(state), // access_token, error + ...toRefs(state), // accessToken, error } } diff --git a/src/router.ts b/src/router.ts index a0602ab..f8aa2bf 100644 --- a/src/router.ts +++ b/src/router.ts @@ -35,13 +35,13 @@ const router = createRouter({ }); router.beforeEach((to, from, next) => { - const { access_token } = useAuth(); + const { accessToken } = useAuth(); // Not logged into a guarded route? - if (to.meta.requiresAuth && !access_token?.value) next({ name: 'login' }); + if (to.meta.requiresAuth && !accessToken?.value) next({ name: 'login' }); // Logged in for an auth route - else if ((to.name == 'login' || to.name == 'register') && access_token!.value) next({ name: 'home' }); + else if ((to.name == 'login' || to.name == 'register') && accessToken!.value) next({ name: 'home' }); // Carry On... else next(); diff --git a/src/views/login.vue b/src/views/login.vue index edc5096..803c516 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -21,7 +21,7 @@