mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-19 01:03:57 +00:00
chore: merge with main
This commit is contained in:
@@ -1,7 +1,24 @@
|
||||
import i18next from "i18next";
|
||||
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) =>
|
||||
WindowManager.openAuthWindow();
|
||||
const openAuthWindow = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
page: AuthPage
|
||||
) => {
|
||||
const searchParams = new URLSearchParams({
|
||||
lng: i18next.language,
|
||||
});
|
||||
|
||||
if ([AuthPage.UpdateEmail, AuthPage.UpdatePassword].includes(page)) {
|
||||
const { accessToken } = await HydraApi.refreshToken().catch(() => {
|
||||
return { accessToken: "" };
|
||||
});
|
||||
searchParams.set("token", accessToken);
|
||||
}
|
||||
|
||||
WindowManager.openAuthWindow(page, searchParams);
|
||||
};
|
||||
|
||||
registerEvent("openAuthWindow", openAuthWindow);
|
||||
|
||||
@@ -221,43 +221,47 @@ export class HydraApi {
|
||||
}
|
||||
}
|
||||
|
||||
private static async revalidateAccessTokenIfExpired() {
|
||||
const now = new Date();
|
||||
public static async refreshToken() {
|
||||
const response = await this.instance.post(`/auth/refresh`, {
|
||||
refreshToken: this.userAuth.refreshToken,
|
||||
});
|
||||
|
||||
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
||||
try {
|
||||
const response = await this.instance.post(`/auth/refresh`, {
|
||||
refreshToken: this.userAuth.refreshToken,
|
||||
});
|
||||
const { accessToken, expiresIn } = response.data;
|
||||
|
||||
const { accessToken, expiresIn } = response.data;
|
||||
const tokenExpirationTimestamp =
|
||||
Date.now() +
|
||||
this.secondsToMilliseconds(expiresIn) -
|
||||
this.EXPIRATION_OFFSET_IN_MS;
|
||||
|
||||
const tokenExpirationTimestamp =
|
||||
now.getTime() +
|
||||
this.secondsToMilliseconds(expiresIn) -
|
||||
this.EXPIRATION_OFFSET_IN_MS;
|
||||
this.userAuth.authToken = accessToken;
|
||||
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
|
||||
|
||||
this.userAuth.authToken = accessToken;
|
||||
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
|
||||
logger.log(
|
||||
"Token refreshed. New expiration:",
|
||||
this.userAuth.expirationTimestamp
|
||||
);
|
||||
|
||||
logger.log(
|
||||
"Token refreshed. New expiration:",
|
||||
this.userAuth.expirationTimestamp
|
||||
await db
|
||||
.get<string, Auth>(levelKeys.auth, { valueEncoding: "json" })
|
||||
.then((auth) => {
|
||||
return db.put<string, Auth>(
|
||||
levelKeys.auth,
|
||||
{
|
||||
...auth,
|
||||
accessToken: Crypto.encrypt(accessToken),
|
||||
tokenExpirationTimestamp,
|
||||
},
|
||||
{ valueEncoding: "json" }
|
||||
);
|
||||
});
|
||||
|
||||
await db
|
||||
.get<string, Auth>(levelKeys.auth, { valueEncoding: "json" })
|
||||
.then((auth) => {
|
||||
return db.put<string, Auth>(
|
||||
levelKeys.auth,
|
||||
{
|
||||
...auth,
|
||||
accessToken: Crypto.encrypt(accessToken),
|
||||
tokenExpirationTimestamp,
|
||||
},
|
||||
{ valueEncoding: "json" }
|
||||
);
|
||||
});
|
||||
return { accessToken, expiresIn };
|
||||
}
|
||||
|
||||
private static async revalidateAccessTokenIfExpired() {
|
||||
if (this.userAuth.expirationTimestamp < Date.now()) {
|
||||
try {
|
||||
await this.refreshToken();
|
||||
} catch (err) {
|
||||
this.handleUnauthorizedError(err);
|
||||
}
|
||||
@@ -272,7 +276,7 @@ export class HydraApi {
|
||||
};
|
||||
}
|
||||
|
||||
private static handleUnauthorizedError = (err) => {
|
||||
private static readonly handleUnauthorizedError = (err) => {
|
||||
if (err instanceof AxiosError && err.response?.status === 401) {
|
||||
logger.error(
|
||||
"401 - Current credentials:",
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
shell,
|
||||
} from "electron";
|
||||
import { is } from "@electron-toolkit/utils";
|
||||
import i18next, { t } from "i18next";
|
||||
import { t } from "i18next";
|
||||
import path from "node:path";
|
||||
import icon from "@resources/icon.png?asset";
|
||||
import trayIcon from "@resources/tray-icon.png?asset";
|
||||
@@ -18,6 +18,7 @@ import UserAgent from "user-agents";
|
||||
import { db, gamesSublevel, levelKeys } from "@main/level";
|
||||
import { slice, sortBy } from "lodash-es";
|
||||
import type { UserPreferences } from "@types";
|
||||
import { AuthPage } from "@shared";
|
||||
|
||||
export class WindowManager {
|
||||
public static mainWindow: Electron.BrowserWindow | null = null;
|
||||
@@ -146,7 +147,7 @@ export class WindowManager {
|
||||
});
|
||||
}
|
||||
|
||||
public static openAuthWindow() {
|
||||
public static openAuthWindow(page: AuthPage, searchParams: URLSearchParams) {
|
||||
if (this.mainWindow) {
|
||||
const authWindow = new BrowserWindow({
|
||||
width: 600,
|
||||
@@ -168,12 +169,8 @@ export class WindowManager {
|
||||
|
||||
if (!app.isPackaged) authWindow.webContents.openDevTools();
|
||||
|
||||
const searchParams = new URLSearchParams({
|
||||
lng: i18next.language,
|
||||
});
|
||||
|
||||
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", () => {
|
||||
@@ -185,6 +182,13 @@ export class WindowManager {
|
||||
authWindow.close();
|
||||
|
||||
HydraApi.handleExternalAuth(url);
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.startsWith("hydralauncher://update-account")) {
|
||||
authWindow.close();
|
||||
|
||||
WindowManager.mainWindow?.webContents.send("on-account-updated");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user