diff --git a/src/main/events/auth/open-auth-window.ts b/src/main/events/auth/open-auth-window.ts new file mode 100644 index 00000000..e93a5a42 --- /dev/null +++ b/src/main/events/auth/open-auth-window.ts @@ -0,0 +1,7 @@ +import { registerEvent } from "../register-event"; +import { WindowManager } from "@main/services"; + +const openAuthWindow = async (_event: Electron.IpcMainInvokeEvent) => + WindowManager.openAuthWindow(); + +registerEvent("openAuthWindow", openAuthWindow); diff --git a/src/main/events/index.ts b/src/main/events/index.ts index 548106e0..32c9243c 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -41,6 +41,7 @@ import "./download-sources/add-download-source"; import "./download-sources/remove-download-source"; import "./download-sources/sync-download-sources"; import "./auth/signout"; +import "./auth/open-auth-window"; import "./user/get-user"; import "./profile/get-me"; import "./profile/update-profile"; diff --git a/src/main/index.ts b/src/main/index.ts index 10399beb..6b51f978 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -8,7 +8,6 @@ import { DownloadManager, logger, WindowManager } from "@main/services"; import { dataSource } from "@main/data-source"; import * as resources from "@locales"; import { userPreferencesRepository } from "@main/repository"; -import { HydraApi } from "./services/hydra-api"; const { autoUpdater } = updater; @@ -87,11 +86,7 @@ app.on("second-instance", (_event, commandLine) => { const [, path] = commandLine.pop()?.split("://") ?? []; if (path) { - if (path.startsWith("auth")) { - HydraApi.handleExternalAuth(path); - } else { - WindowManager.redirect(path); - } + WindowManager.redirect(path); } }); diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index 8e50e646..e6e042db 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -23,8 +23,8 @@ export class HydraApi { return this.userAuth.authToken !== ""; } - static async handleExternalAuth(auth: string) { - const { payload } = url.parse(auth, true).query; + static async handleExternalAuth(uri: string) { + const { payload } = url.parse(uri, true).query; const decodedBase64 = atob(payload as string); const jsonData = JSON.parse(decodedBase64); diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index 7b57445b..eccceccf 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -15,6 +15,7 @@ import icon from "@resources/icon.png?asset"; import trayIcon from "@resources/tray-icon.png?asset"; import { gameRepository, userPreferencesRepository } from "@main/repository"; import { IsNull, Not } from "typeorm"; +import { HydraApi } from "./hydra-api"; export class WindowManager { public static mainWindow: Electron.BrowserWindow | null = null; @@ -80,6 +81,41 @@ export class WindowManager { }); } + public static openAuthWindow() { + if (this.mainWindow) { + const authWindow = new BrowserWindow({ + width: 600, + height: 640, + backgroundColor: "#1c1c1c", + parent: this.mainWindow, + modal: true, + show: false, + maximizable: false, + resizable: false, + minimizable: false, + webPreferences: { + sandbox: false, + }, + }); + + authWindow.removeMenu(); + + authWindow.loadURL("http://localhost:3000"); + + authWindow.once("ready-to-show", () => { + authWindow.show(); + }); + + authWindow.webContents.on("will-navigate", (_event, url) => { + if (url.startsWith("hydralauncher://auth")) { + authWindow.close(); + + HydraApi.handleExternalAuth(url); + } + }); + } + } + public static redirect(hash: string) { if (!this.mainWindow) this.createMainWindow(); this.loadURL(hash); diff --git a/src/preload/index.ts b/src/preload/index.ts index dd03f114..0cacafe3 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -143,6 +143,7 @@ contextBridge.exposeInMainWorld("electron", { /* Auth */ signOut: () => ipcRenderer.invoke("signOut"), + openAuthWindow: () => ipcRenderer.invoke("openAuthWindow"), onSignIn: (cb: () => void) => { const listener = (_event: Electron.IpcRendererEvent) => cb(); ipcRenderer.on("on-signin", listener); diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index 332a846b..27671a23 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -113,7 +113,7 @@ export function App() { return () => { listeners.forEach((unsubscribe) => unsubscribe()); }; - }, [fetchUserDetails, updateUserDetails, clearUserDetails]); + }, [fetchUserDetails, updateUserDetails, updateLibrary, clearUserDetails]); const handleSearch = useCallback( (query: string) => { diff --git a/src/renderer/src/components/sidebar/sidebar-profile.tsx b/src/renderer/src/components/sidebar/sidebar-profile.tsx index 4a89eff1..c86aecb7 100644 --- a/src/renderer/src/components/sidebar/sidebar-profile.tsx +++ b/src/renderer/src/components/sidebar/sidebar-profile.tsx @@ -12,7 +12,7 @@ export function SidebarProfile() { const handleButtonClick = () => { if (userDetails === null) { - window.electron.openExternal("https://auth.hydra.losbroxas.org"); + window.electron.openAuthWindow(); return; } diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 6a0160c9..b7707d6f 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -115,6 +115,7 @@ declare global { /* Auth */ signOut: () => Promise; + openAuthWindow: () => Promise; onSignIn: (cb: () => void) => () => Electron.IpcRenderer; onSignOut: (cb: () => void) => () => Electron.IpcRenderer;