mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-24 03:11:03 +00:00
feat: loading from me endpoint and updating sidebar profile
This commit is contained in:
@@ -11,6 +11,15 @@ export class UserAuth {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
userId: string;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
displayName: string;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
profileImageUrl: string;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
accessToken: string;
|
||||
|
||||
|
||||
@@ -12,20 +12,3 @@ export const downloadSourceSchema = z.object({
|
||||
})
|
||||
),
|
||||
});
|
||||
|
||||
const gamesArray = z.array(
|
||||
z.object({
|
||||
id: z.string().length(8),
|
||||
objectId: z.string().max(255),
|
||||
playTimeInSeconds: z.number().int(),
|
||||
shop: z.enum(["steam", "epic"]),
|
||||
lastTimePlayed: z.coerce.date().nullable(),
|
||||
})
|
||||
);
|
||||
|
||||
export const userProfileSchema = z.object({
|
||||
displayName: z.string(),
|
||||
profileImageUrl: z.string().url().nullable(),
|
||||
libraryGames: gamesArray,
|
||||
recentGames: gamesArray,
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { defaultDownloadsPath } from "@main/constants";
|
||||
import { app, ipcMain } from "electron";
|
||||
import { HydraApi } from "@main/services/hydra-api";
|
||||
|
||||
import "./catalogue/get-catalogue";
|
||||
import "./catalogue/get-game-shop-details";
|
||||
@@ -40,7 +41,9 @@ import "./download-sources/add-download-source";
|
||||
import "./download-sources/remove-download-source";
|
||||
import "./download-sources/sync-download-sources";
|
||||
import "./profile/get-user-profile";
|
||||
import "./profile/get-me";
|
||||
|
||||
ipcMain.handle("ping", () => "pong");
|
||||
ipcMain.handle("getVersion", () => app.getVersion());
|
||||
ipcMain.handle("isUserLoggedIn", () => HydraApi.isLoggedIn());
|
||||
ipcMain.handle("getDefaultDownloadsPath", () => defaultDownloadsPath);
|
||||
|
||||
30
src/main/events/profile/get-me.ts
Normal file
30
src/main/events/profile/get-me.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services/hydra-api";
|
||||
import { UserProfile } from "@types";
|
||||
import { userAuthRepository } from "@main/repository";
|
||||
|
||||
const getMe = async (
|
||||
_event: Electron.IpcMainInvokeEvent
|
||||
): Promise<UserProfile | null> => {
|
||||
return HydraApi.get(`/profile/me`)
|
||||
.then((response) => {
|
||||
const me = response.data;
|
||||
|
||||
userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
displayName: me.displayName,
|
||||
profileImageUrl: me.displayName,
|
||||
userId: me.id,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
|
||||
return me;
|
||||
})
|
||||
.catch(() => {
|
||||
return userAuthRepository.findOne({ where: { id: 1 } });
|
||||
});
|
||||
};
|
||||
|
||||
registerEvent("getMe", getMe);
|
||||
@@ -1,6 +1,4 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { userProfileSchema } from "../helpers/validators";
|
||||
import { logger } from "@main/services";
|
||||
import { HydraApi } from "@main/services/hydra-api";
|
||||
import { steamGamesWorker } from "@main/workers";
|
||||
import { UserProfile } from "@types";
|
||||
@@ -13,7 +11,7 @@ const getUserProfile = async (
|
||||
): Promise<UserProfile | null> => {
|
||||
try {
|
||||
const response = await HydraApi.get(`/user/${username}`);
|
||||
const profile = userProfileSchema.parse(response.data);
|
||||
const profile = response.data;
|
||||
|
||||
const recentGames = await Promise.all(
|
||||
profile.recentGames.map(async (game) => {
|
||||
@@ -51,7 +49,6 @@ const getUserProfile = async (
|
||||
|
||||
return { ...profile, libraryGames, recentGames };
|
||||
} catch (err) {
|
||||
logger.error(`getUserProfile: ${username}`, err);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { userAuthRepository } from "@main/repository";
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import axios, { AxiosError, AxiosInstance } from "axios";
|
||||
|
||||
export class HydraApi {
|
||||
private static instance: AxiosInstance;
|
||||
@@ -12,6 +12,10 @@ export class HydraApi {
|
||||
expirationTimestamp: 0,
|
||||
};
|
||||
|
||||
static isLoggedIn() {
|
||||
return this.userAuth.authToken !== "";
|
||||
}
|
||||
|
||||
static async setupApi() {
|
||||
this.instance = axios.create({
|
||||
baseURL: import.meta.env.MAIN_VITE_API_URL,
|
||||
@@ -31,26 +35,48 @@ export class HydraApi {
|
||||
private static async revalidateAccessTokenIfExpired() {
|
||||
const now = new Date();
|
||||
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
||||
const response = await this.instance.post(`/auth/refresh`, {
|
||||
refreshToken: this.userAuth.refreshToken,
|
||||
});
|
||||
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 =
|
||||
now.getTime() + expiresIn - this.EXPIRATION_OFFSET_IN_MS;
|
||||
const tokenExpirationTimestamp =
|
||||
now.getTime() + expiresIn - this.EXPIRATION_OFFSET_IN_MS;
|
||||
|
||||
this.userAuth.authToken = accessToken;
|
||||
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
|
||||
this.userAuth.authToken = accessToken;
|
||||
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
|
||||
|
||||
userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
accessToken,
|
||||
tokenExpirationTimestamp,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
accessToken,
|
||||
tokenExpirationTimestamp,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof AxiosError &&
|
||||
(err?.response?.status === 401 || err?.response?.status === 403)
|
||||
) {
|
||||
this.userAuth.authToken = "";
|
||||
this.userAuth.expirationTimestamp = 0;
|
||||
|
||||
userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
accessToken: "",
|
||||
refreshToken: "",
|
||||
tokenExpirationTimestamp: 0,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user