feat: refactor open auth

This commit is contained in:
Zamitto 2025-01-15 23:56:37 -03:00
parent 9941460c60
commit 44fd971c95
13 changed files with 47 additions and 57 deletions

View File

@ -291,7 +291,7 @@
"update_email": "Update email", "update_email": "Update email",
"update_password": "Update password", "update_password": "Update password",
"current_email": "Current email:", "current_email": "Current email:",
"no_associated_email": "You don't have an associated email yet" "no_email_account": "You have not set an email yet"
}, },
"notifications": { "notifications": {
"download_complete": "Download complete", "download_complete": "Download complete",

View File

@ -279,7 +279,7 @@
"update_email": "Atualizar email", "update_email": "Atualizar email",
"update_password": "Atualizar senha", "update_password": "Atualizar senha",
"current_email": "Email atual:", "current_email": "Email atual:",
"no_associated_email": "Você ainda não associou nenhum email a sua conta" "no_email_account": "Você ainda não adicionou um email a sua conta"
}, },
"notifications": { "notifications": {
"download_complete": "Download concluído", "download_complete": "Download concluído",

View File

@ -1,7 +1,22 @@
import i18next from "i18next";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { WindowManager } from "@main/services"; import { HydraApi, WindowManager } from "@main/services";
import { AuthPage } from "@shared";
const openAuthWindow = async (_event: Electron.IpcMainInvokeEvent) => const openAuthWindow = async (
WindowManager.openAuthWindow(); _event: Electron.IpcMainInvokeEvent,
page: AuthPage
) => {
const searchParams = new URLSearchParams({
lng: i18next.language,
});
if ([AuthPage.UpdateEmail, AuthPage.UpdatePassword].includes(page)) {
const { accessToken } = await HydraApi.refreshToken();
searchParams.set("token", accessToken);
}
return WindowManager.openAuthWindow(page, searchParams);
};
registerEvent("openAuthWindow", openAuthWindow); registerEvent("openAuthWindow", openAuthWindow);

View File

@ -30,7 +30,6 @@ import "./library/remove-game-from-library";
import "./library/select-game-wine-prefix"; import "./library/select-game-wine-prefix";
import "./library/reset-game-achievements"; import "./library/reset-game-achievements";
import "./misc/open-checkout"; import "./misc/open-checkout";
import "./misc/open-manage-account";
import "./misc/open-external"; import "./misc/open-external";
import "./misc/show-open-dialog"; import "./misc/show-open-dialog";
import "./misc/get-features"; import "./misc/get-features";

View File

@ -1,25 +0,0 @@
import { shell } from "electron";
import { registerEvent } from "../register-event";
import { HydraApi, logger } from "@main/services";
import { ManageAccountPage } from "@types";
const openManageAccount = async (
_event: Electron.IpcMainInvokeEvent,
page: ManageAccountPage
) => {
try {
const { accessToken } = await HydraApi.refreshToken();
const params = new URLSearchParams({
token: accessToken,
});
shell.openExternal(
`${import.meta.env.MAIN_VITE_AUTH_URL}/${page}?${params.toString()}`
);
} catch (err) {
logger.error("Failed to open manage account", err);
}
};
registerEvent("openManageAccount", openManageAccount);

View File

@ -9,7 +9,7 @@ import {
shell, shell,
} from "electron"; } from "electron";
import { is } from "@electron-toolkit/utils"; import { is } from "@electron-toolkit/utils";
import i18next, { t } from "i18next"; import { t } from "i18next";
import path from "node:path"; import path from "node:path";
import icon from "@resources/icon.png?asset"; import icon from "@resources/icon.png?asset";
import trayIcon from "@resources/tray-icon.png?asset"; import trayIcon from "@resources/tray-icon.png?asset";
@ -17,6 +17,7 @@ import { gameRepository, userPreferencesRepository } from "@main/repository";
import { IsNull, Not } from "typeorm"; import { IsNull, Not } from "typeorm";
import { HydraApi } from "./hydra-api"; import { HydraApi } from "./hydra-api";
import UserAgent from "user-agents"; import UserAgent from "user-agents";
import { AuthPage } from "@shared";
export class WindowManager { export class WindowManager {
public static mainWindow: Electron.BrowserWindow | null = null; public static mainWindow: Electron.BrowserWindow | null = null;
@ -142,7 +143,7 @@ export class WindowManager {
}); });
} }
public static openAuthWindow() { public static openAuthWindow(page: AuthPage, searchParams: URLSearchParams) {
if (this.mainWindow) { if (this.mainWindow) {
const authWindow = new BrowserWindow({ const authWindow = new BrowserWindow({
width: 600, width: 600,
@ -164,12 +165,8 @@ export class WindowManager {
if (!app.isPackaged) authWindow.webContents.openDevTools(); if (!app.isPackaged) authWindow.webContents.openDevTools();
const searchParams = new URLSearchParams({
lng: i18next.language,
});
authWindow.loadURL( authWindow.loadURL(
`${import.meta.env.MAIN_VITE_AUTH_URL}/?${searchParams.toString()}` `${import.meta.env.MAIN_VITE_AUTH_URL}/${page}?${searchParams.toString()}`
); );
authWindow.once("ready-to-show", () => { authWindow.once("ready-to-show", () => {

View File

@ -14,9 +14,8 @@ import type {
CatalogueSearchPayload, CatalogueSearchPayload,
SeedingStatus, SeedingStatus,
GameAchievement, GameAchievement,
ManageAccountPage,
} from "@types"; } from "@types";
import type { CatalogueCategory } from "@shared"; import type { AuthPage, CatalogueCategory } from "@shared";
import type { AxiosProgressEvent } from "axios"; import type { AxiosProgressEvent } from "axios";
contextBridge.exposeInMainWorld("electron", { contextBridge.exposeInMainWorld("electron", {
@ -227,8 +226,6 @@ contextBridge.exposeInMainWorld("electron", {
isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"), isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"),
openExternal: (src: string) => ipcRenderer.invoke("openExternal", src), openExternal: (src: string) => ipcRenderer.invoke("openExternal", src),
openCheckout: () => ipcRenderer.invoke("openCheckout"), openCheckout: () => ipcRenderer.invoke("openCheckout"),
openManageAccount: (page: ManageAccountPage) =>
ipcRenderer.invoke("openManageAccount", page),
showOpenDialog: (options: Electron.OpenDialogOptions) => showOpenDialog: (options: Electron.OpenDialogOptions) =>
ipcRenderer.invoke("showOpenDialog", options), ipcRenderer.invoke("showOpenDialog", options),
showItemInFolder: (path: string) => showItemInFolder: (path: string) =>
@ -294,7 +291,8 @@ contextBridge.exposeInMainWorld("electron", {
/* Auth */ /* Auth */
signOut: () => ipcRenderer.invoke("signOut"), signOut: () => ipcRenderer.invoke("signOut"),
openAuthWindow: () => ipcRenderer.invoke("openAuthWindow"), openAuthWindow: (page: AuthPage) =>
ipcRenderer.invoke("openAuthWindow", page),
getSessionHash: () => ipcRenderer.invoke("getSessionHash"), getSessionHash: () => ipcRenderer.invoke("getSessionHash"),
onSignIn: (cb: () => void) => { onSignIn: (cb: () => void) => {
const listener = (_event: Electron.IpcRendererEvent) => cb(); const listener = (_event: Electron.IpcRendererEvent) => cb();

View File

@ -7,6 +7,7 @@ import { useTranslation } from "react-i18next";
import { UserFriendModalTab } from "@renderer/pages/shared-modals/user-friend-modal"; import { UserFriendModalTab } from "@renderer/pages/shared-modals/user-friend-modal";
import SteamLogo from "@renderer/assets/steam-logo.svg?react"; import SteamLogo from "@renderer/assets/steam-logo.svg?react";
import { Avatar } from "../avatar/avatar"; import { Avatar } from "../avatar/avatar";
import { AuthPage } from "@shared";
const LONG_POLLING_INTERVAL = 120_000; const LONG_POLLING_INTERVAL = 120_000;
@ -26,11 +27,11 @@ export function SidebarProfile() {
const handleProfileClick = () => { const handleProfileClick = () => {
if (userDetails === null) { if (userDetails === null) {
window.electron.openAuthWindow(); window.electron.openAuthWindow(AuthPage.SignIn);
return; return;
} }
navigate(`/profile/${userDetails!.id}`); navigate(`/profile/${userDetails.id}`);
}; };
useEffect(() => { useEffect(() => {

View File

@ -1,4 +1,4 @@
import type { CatalogueCategory } from "@shared"; import type { AuthPage, CatalogueCategory } from "@shared";
import type { import type {
AppUpdaterEvent, AppUpdaterEvent,
Game, Game,
@ -29,7 +29,6 @@ import type {
UserAchievement, UserAchievement,
ComparedAchievements, ComparedAchievements,
CatalogueSearchPayload, CatalogueSearchPayload,
ManageAccountPage,
} from "@types"; } from "@types";
import type { AxiosProgressEvent } from "axios"; import type { AxiosProgressEvent } from "axios";
import type disk from "diskusage"; import type disk from "diskusage";
@ -188,7 +187,6 @@ declare global {
/* Misc */ /* Misc */
openExternal: (src: string) => Promise<void>; openExternal: (src: string) => Promise<void>;
openCheckout: () => Promise<void>; openCheckout: () => Promise<void>;
openManageAccount: (page: ManageAccountPage) => Promise<void>;
getVersion: () => Promise<string>; getVersion: () => Promise<string>;
isStaging: () => Promise<boolean>; isStaging: () => Promise<boolean>;
ping: () => string; ping: () => string;
@ -210,7 +208,7 @@ declare global {
/* Auth */ /* Auth */
signOut: () => Promise<void>; signOut: () => Promise<void>;
openAuthWindow: () => Promise<void>; openAuthWindow: (page: AuthPage) => Promise<void>;
getSessionHash: () => Promise<string | null>; getSessionHash: () => Promise<string | null>;
onSignIn: (cb: () => void) => () => Electron.IpcRenderer; onSignIn: (cb: () => void) => () => Electron.IpcRenderer;
onSignOut: (cb: () => void) => () => Electron.IpcRenderer; onSignOut: (cb: () => void) => () => Electron.IpcRenderer;

View File

@ -10,7 +10,7 @@ import { Sidebar } from "./sidebar/sidebar";
import * as styles from "./game-details.css"; import * as styles from "./game-details.css";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { cloudSyncContext, gameDetailsContext } from "@renderer/context"; import { cloudSyncContext, gameDetailsContext } from "@renderer/context";
import { steamUrlBuilder } from "@shared"; import { AuthPage, steamUrlBuilder } from "@shared";
import cloudIconAnimated from "@renderer/assets/icons/cloud-animated.gif"; import cloudIconAnimated from "@renderer/assets/icons/cloud-animated.gif";
import { useUserDetails } from "@renderer/hooks"; import { useUserDetails } from "@renderer/hooks";
@ -69,7 +69,7 @@ export function GameDetailsContent() {
}); });
const backgroundColor = output const backgroundColor = output
? (new Color(output).darken(0.7).toString() as string) ? new Color(output).darken(0.7).toString()
: ""; : "";
setGameColor(backgroundColor); setGameColor(backgroundColor);
@ -101,7 +101,7 @@ export function GameDetailsContent() {
const handleCloudSaveButtonClick = () => { const handleCloudSaveButtonClick = () => {
if (!userDetails) { if (!userDetails) {
window.electron.openAuthWindow(); window.electron.openAuthWindow(AuthPage.SignIn);
return; return;
} }

View File

@ -13,6 +13,7 @@ import {
XCircleFillIcon, XCircleFillIcon,
} from "@primer/octicons-react"; } from "@primer/octicons-react";
import { settingsContext } from "@renderer/context"; import { settingsContext } from "@renderer/context";
import { AuthPage } from "@shared";
interface FormValues { interface FormValues {
profileVisibility: "PUBLIC" | "FRIENDS" | "PRIVATE"; profileVisibility: "PUBLIC" | "FRIENDS" | "PRIVATE";
@ -128,7 +129,7 @@ export function SettingsAccount() {
<p>{userDetails.email}</p> <p>{userDetails.email}</p>
</div> </div>
) : ( ) : (
<p>{t("no_associated_email")}</p> <p>{t("no_email_account")}</p>
)} )}
<div <div
@ -142,7 +143,7 @@ export function SettingsAccount() {
> >
<Button <Button
theme="outline" theme="outline"
onClick={() => window.electron.openManageAccount("update-email")} onClick={() => window.electron.openAuthWindow(AuthPage.UpdateEmail)}
> >
{t("update_email")} {t("update_email")}
<MailIcon /> <MailIcon />
@ -150,7 +151,9 @@ export function SettingsAccount() {
<Button <Button
theme="outline" theme="outline"
onClick={() => window.electron.openManageAccount("update-password")} onClick={() =>
window.electron.openAuthWindow(AuthPage.UpdatePassword)
}
> >
{t("update_password")} {t("update_password")}
<KeyIcon /> <KeyIcon />

View File

@ -42,3 +42,9 @@ export enum Cracker {
rle = "RLE", rle = "RLE",
razor1911 = "RAZOR1911", razor1911 = "RAZOR1911",
} }
export enum AuthPage {
SignIn = "",
UpdateEmail = "update-email",
UpdatePassword = "update-password",
}

View File

@ -416,8 +416,6 @@ export interface CatalogueSearchPayload {
developers: string[]; developers: string[];
} }
export type ManageAccountPage = "update-email" | "update-password";
export * from "./steam.types"; export * from "./steam.types";
export * from "./real-debrid.types"; export * from "./real-debrid.types";
export * from "./ludusavi.types"; export * from "./ludusavi.types";