import { app, BrowserWindow, dialog, session } from "electron"; import { ipcMain } from "electron"; import { join } from "path"; import { accessTokenName, getAdminAccessTokenValue, getAdminRefreshTokenValue, refreshTokenName } from "./src/login"; import { UserAccessor } from "./src/model/mod"; import { create_server } from "./src/server"; import { get_setting } from "./src/SettingConfig"; function registerChannel(cntr: UserAccessor) { ipcMain.handle("reset_password", async (event, username: string, password: string) => { const user = await cntr.findUser(username); if (user === undefined) { return false; } user.reset_password(password); return true; }); } const setting = get_setting(); if (!setting.cli) { let wnd: BrowserWindow | null = null; const createWindow = async () => { wnd = new BrowserWindow({ width: 800, height: 600, center: true, useContentSize: true, webPreferences: { preload: join(__dirname, "preload.js"), contextIsolation: true, }, }); await wnd.loadURL(`data:text/html;base64,` + Buffer.from(loading_html).toString("base64")); // await wnd.loadURL('../loading.html'); // set admin cookies. await session.defaultSession.cookies.set({ url: `http://localhost:${setting.port}`, name: accessTokenName, value: getAdminAccessTokenValue(), httpOnly: true, secure: false, sameSite: "strict", }); await session.defaultSession.cookies.set({ url: `http://localhost:${setting.port}`, name: refreshTokenName, value: getAdminRefreshTokenValue(), httpOnly: true, secure: false, sameSite: "strict", }); try { const server = await create_server(); const app = server.start_server(); registerChannel(server.userController); await wnd.loadURL(`http://localhost:${setting.port}`); } catch (e) { if (e instanceof Error) { await dialog.showMessageBox({ type: "error", title: "error!", message: e.message, }); } else { await dialog.showMessageBox({ type: "error", title: "error!", message: String(e), }); } } wnd.on("closed", () => { wnd = null; }); }; const isPrimary = app.requestSingleInstanceLock(); if (!isPrimary) { app.quit(); // exit window app.exit(); } app.on("second-instance", () => { if (wnd != null) { if (wnd.isMinimized()) { wnd.restore(); } wnd.focus(); } }); app.on("ready", (event, info) => { createWindow(); }); app.on("window-all-closed", () => { // quit when all windows are closed if (process.platform != "darwin") app.quit(); // (except leave MacOS app active until Cmd+Q) }); app.on("activate", () => { // re-recreate window when dock icon is clicked and no other windows open if (wnd == null) createWindow(); }); } else { (async () => { try { const server = await create_server(); server.start_server(); } catch (error) { console.log(error); } })(); } const loading_html = `