feat: adding auth window

This commit is contained in:
Chubby Granny Chaser 2024-06-20 13:22:12 +01:00
parent 884ba47b9b
commit 71d79a4a15
No known key found for this signature in database
9 changed files with 51 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -113,7 +113,7 @@ export function App() {
return () => {
listeners.forEach((unsubscribe) => unsubscribe());
};
}, [fetchUserDetails, updateUserDetails, clearUserDetails]);
}, [fetchUserDetails, updateUserDetails, updateLibrary, clearUserDetails]);
const handleSearch = useCallback(
(query: string) => {

View File

@ -12,7 +12,7 @@ export function SidebarProfile() {
const handleButtonClick = () => {
if (userDetails === null) {
window.electron.openExternal("https://auth.hydra.losbroxas.org");
window.electron.openAuthWindow();
return;
}

View File

@ -115,6 +115,7 @@ declare global {
/* Auth */
signOut: () => Promise<void>;
openAuthWindow: () => Promise<void>;
onSignIn: (cb: () => void) => () => Electron.IpcRenderer;
onSignOut: (cb: () => void) => () => Electron.IpcRenderer;