Skip to content

Commit

Permalink
fix: error with multiple sessions in non-dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Nov 13, 2023
1 parent 075f542 commit 169f610
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 71 deletions.
121 changes: 57 additions & 64 deletions src/main/main.ts
Expand Up @@ -78,87 +78,80 @@ async function createMainWindow () {
return window;
}

if (!gotTheLock && !safeStorage.isEncryptionAvailable()) // Disable multiple instances if is not possible to share session keys
app.quit();
else {
require('@electron/remote/main').initialize();

// Initialize ipcHandlers
ipcHandlers();

ipcMain.on('refresh-theme-settings', () => {
const appTheme = settingsStore.get('application_theme');
if (isWindows && mainWindow) {
mainWindow.setTitleBarOverlay({
color: appTheme === 'dark' ? '#3f3f3f' : '#fff',
symbolColor: appTheme === 'dark' ? '#fff' : '#000'
});
}
});
require('@electron/remote/main').initialize();

ipcMain.on('change-window-title', (_, title: string) => {
if (mainWindow) mainWindow.setTitle(title);
});
// Initialize ipcHandlers
ipcHandlers();

// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (!isMacOS) app.quit();
});
ipcMain.on('refresh-theme-settings', () => {
const appTheme = settingsStore.get('application_theme');
if (isWindows && mainWindow) {
mainWindow.setTitleBarOverlay({
color: appTheme === 'dark' ? '#3f3f3f' : '#fff',
symbolColor: appTheme === 'dark' ? '#fff' : '#000'
});
}
});

app.on('activate', async () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null)
mainWindow = await createMainWindow();
});
ipcMain.on('change-window-title', (_, title: string) => {
if (mainWindow) mainWindow.setTitle(title);
});

// create main BrowserWindow when electron is ready
app.on('ready', async () => {
mainWindowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 800
});
// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (!isMacOS) app.quit();
});

app.on('activate', async () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null)
mainWindow = await createMainWindow();
createAppMenu();
});

// create main BrowserWindow when electron is ready
app.on('ready', async () => {
if (!gotTheLock && !safeStorage.isEncryptionAvailable()) // Disable multiple instances if is not possible to share session keys
app.quit();

if (isWindows)
mainWindow.show();
mainWindowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 800
});

// if (isDevelopment)
// mainWindow.webContents.openDevTools();
mainWindow = await createMainWindow();
createAppMenu();

// const key = safeStorage.encryptString('godisnothere');
// console.log('KEY:', key.toString('hex'));
if (isWindows)
mainWindow.show();

// const decrypted = safeStorage.decryptString(key);
// console.log(decrypted.toString());
// if (isDevelopment)
// mainWindow.webContents.openDevTools();

process.on('uncaughtException', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
process.on('uncaughtException', error => {
mainWindow.webContents.send('unhandled-exception', error);
});

process.on('unhandledRejection', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
process.on('unhandledRejection', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
});

app.on('browser-window-created', (event, window) => {
if (isDevelopment) {
const { antares } = require('../../package.json');
const extensionPath = path.resolve(__dirname, `../../misc/${antares.devtoolsId}`);
window.webContents.session.loadExtension(extensionPath, { allowFileAccess: true }).catch(console.error);
}
app.on('browser-window-created', (event, window) => {
if (isDevelopment) {
const { antares } = require('../../package.json');
const extensionPath = path.resolve(__dirname, `../../misc/${antares.devtoolsId}`);
window.webContents.session.loadExtension(extensionPath, { allowFileAccess: true }).catch(console.error);
}

window.webContents.on('will-navigate', (e) => { // Prevent browser navigation
e.preventDefault();
});
window.webContents.on('will-navigate', (e) => { // Prevent browser navigation
e.preventDefault();
});

window.webContents.on('did-create-window', (w) => { // Close new windows
w.close();
});
window.webContents.on('did-create-window', (w) => { // Close new windows
w.close();
});
}
});

function createAppMenu () {
const menuTemplate: OsMenu = {
Expand Down
17 changes: 10 additions & 7 deletions src/renderer/stores/connections.ts
Expand Up @@ -6,7 +6,7 @@ import * as Store from 'electron-store';
import { defineStore } from 'pinia';

import { useWorkspacesStore } from '@/stores/workspaces';
const key = localStorage.getItem('key');
let key = localStorage.getItem('key');

export interface SidebarElement {
isFolder: boolean;
Expand All @@ -18,18 +18,21 @@ export interface SidebarElement {
icon?: null | string;
}

if (!key) {
const storedKey = ipcRenderer.sendSync('get-key');
if (!key) { // If no key in local storace
const storedKey = ipcRenderer.sendSync('get-key');// Ask for key stored on disk

if (!storedKey) {
if (!storedKey) { // Of nop stored key on disk
const newKey = crypto.randomBytes(16).toString('hex');
localStorage.setItem('key', newKey);
key = newKey;
}
else
else {
localStorage.setItem('key', storedKey);
key = storedKey;
}
}

ipcRenderer.send('set-key', key);
else
ipcRenderer.send('set-key', key);

const persistentStore = new Store({
name: 'connections',
Expand Down

0 comments on commit 169f610

Please sign in to comment.