chore: merge with main

This commit is contained in:
Chubby Granny Chaser
2025-01-21 03:51:53 +00:00
17 changed files with 451 additions and 207 deletions

View File

@@ -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:",

View File

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