Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: manage account buttons #1402

Merged
merged 18 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,18 @@
"launch_minimized": "Launch Hydra minimized",
"disable_nsfw_alert": "Disable NSFW alert",
"seed_after_download_complete": "Seed after download complete",
"show_hidden_achievement_description": "Show hidden achievements description before unlocking them"
"show_hidden_achievement_description": "Show hidden achievements description before unlocking them",
"account": "Account",
"no_users_blocked": "You have no blocked users",
"subscription": "Hydra Cloud subscription",
"subscription_active_until": "Your Hydra Cloud is active until {{date}}",
"subscription_not_active": "You don't have an active Hydra Cloud subscription",
zamitto marked this conversation as resolved.
Show resolved Hide resolved
"manage_account": "Manage account",
"manage_subscription": "Manage subscription",
"update_email": "Update email",
"update_password": "Update password",
"current_email": "Current email:",
"no_associated_email": "You don't have an associated email yet"
},
"notifications": {
"download_complete": "Download complete",
Expand Down
13 changes: 12 additions & 1 deletion src/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,18 @@
"launch_minimized": "Iniciar o Hydra minimizado",
"disable_nsfw_alert": "Desativar alerta de conteúdo inapropriado",
"seed_after_download_complete": "Semear após a conclusão do download",
"show_hidden_achievement_description": "Mostrar descrição de conquistas ocultas antes de debloqueá-las"
"show_hidden_achievement_description": "Mostrar descrição de conquistas ocultas antes de debloqueá-las",
zamitto marked this conversation as resolved.
Show resolved Hide resolved
"account": "Conta",
"no_users_blocked": "Você não bloqueou nenhum usuário",
"subscription": "Assinatura Hydra Cloud",
"subscription_active_until": "Seu Hydra Cloud ficará ativo até {{date}}",
"subscription_not_active": "Você não possui uma assinatura Hydra Cloud ativa",
"manage_account": "Gerenciar conta",
"manage_subscription": "Gerenciar assinatura",
"update_email": "Atualizar email",
"update_password": "Atualizar senha",
"current_email": "Email atual:",
"no_associated_email": "Você ainda não associou nenhum email a sua conta"
},
"notifications": {
"download_complete": "Download concluído",
Expand Down
1 change: 1 addition & 0 deletions src/main/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import "./library/remove-game-from-library";
import "./library/select-game-wine-prefix";
import "./library/reset-game-achievements";
import "./misc/open-checkout";
import "./misc/open-manage-account";
import "./misc/open-external";
import "./misc/show-open-dialog";
import "./misc/get-features";
Expand Down
25 changes: 25 additions & 0 deletions src/main/events/misc/open-manage-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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();
zamitto marked this conversation as resolved.
Show resolved Hide resolved

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);
3 changes: 2 additions & 1 deletion src/main/services/hosters/datanodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class DatanodesApi {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
},
maxRedirects: 0, validateStatus: (status: number) => status === 302 || status < 400,
maxRedirects: 0,
validateStatus: (status: number) => status === 302 || status < 400,
}
);

Expand Down
66 changes: 35 additions & 31 deletions src/main/services/hydra-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,38 +215,42 @@ export class HydraApi {
}
}

private static async revalidateAccessTokenIfExpired() {
const now = new Date();
public static async refreshToken() {
zamitto marked this conversation as resolved.
Show resolved Hide resolved
const { accessToken, expiresIn } = await this.instance
.post<{ accessToken: string; expiresIn: number }>(`/auth/refresh`, {
refreshToken: this.userAuth.refreshToken,
})
.then((response) => response.data);

const tokenExpirationTimestamp =
Date.now() +
this.secondsToMilliseconds(expiresIn) -
this.EXPIRATION_OFFSET_IN_MS;

if (this.userAuth.expirationTimestamp < now.getTime()) {
this.userAuth.authToken = accessToken;
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;

logger.log(
"Token refreshed. New expiration:",
this.userAuth.expirationTimestamp
);

userAuthRepository.upsert(
{
id: 1,
accessToken,
tokenExpirationTimestamp,
},
["id"]
);

return { accessToken, expiresIn };
}

private static async revalidateAccessTokenIfExpired() {
if (this.userAuth.expirationTimestamp < Date.now()) {
try {
const response = await this.instance.post(`/auth/refresh`, {
refreshToken: this.userAuth.refreshToken,
});

const { accessToken, expiresIn } = response.data;

const tokenExpirationTimestamp =
now.getTime() +
this.secondsToMilliseconds(expiresIn) -
this.EXPIRATION_OFFSET_IN_MS;

this.userAuth.authToken = accessToken;
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;

logger.log(
"Token refreshed. New expiration:",
this.userAuth.expirationTimestamp
);

userAuthRepository.upsert(
{
id: 1,
accessToken,
tokenExpirationTimestamp,
},
["id"]
);
await this.refreshToken();
} catch (err) {
this.handleUnauthorizedError(err);
}
Expand All @@ -261,7 +265,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:",
Expand Down
3 changes: 3 additions & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
CatalogueSearchPayload,
SeedingStatus,
GameAchievement,
ManageAccountPage,
} from "@types";
import type { CatalogueCategory } from "@shared";
import type { AxiosProgressEvent } from "axios";
Expand Down Expand Up @@ -226,6 +227,8 @@ contextBridge.exposeInMainWorld("electron", {
isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"),
openExternal: (src: string) => ipcRenderer.invoke("openExternal", src),
openCheckout: () => ipcRenderer.invoke("openCheckout"),
openManageAccount: (page: ManageAccountPage) =>
ipcRenderer.invoke("openManageAccount", page),
showOpenDialog: (options: Electron.OpenDialogOptions) =>
ipcRenderer.invoke("showOpenDialog", options),
showItemInFolder: (path: string) =>
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/src/declaration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
UserAchievement,
ComparedAchievements,
CatalogueSearchPayload,
ManageAccountPage,
} from "@types";
import type { AxiosProgressEvent } from "axios";
import type disk from "diskusage";
Expand Down Expand Up @@ -187,6 +188,7 @@ declare global {
/* Misc */
openExternal: (src: string) => Promise<void>;
openCheckout: () => Promise<void>;
openManageAccount: (page: ManageAccountPage) => Promise<void>;
getVersion: () => Promise<string>;
isStaging: () => Promise<boolean>;
ping: () => string;
Expand Down
Loading
Loading