mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-01-23 21:44:55 +03:00
feat: prevent api calls when user is not logged in
This commit is contained in:
parent
9870213fff
commit
dd23358a95
@ -53,18 +53,7 @@ const addGameToLibrary = async (
|
|||||||
|
|
||||||
const game = await gameRepository.findOne({ where: { objectID } });
|
const game = await gameRepository.findOne({ where: { objectID } });
|
||||||
|
|
||||||
createGame(game!).then((response) => {
|
createGame(game!);
|
||||||
const {
|
|
||||||
id: remoteId,
|
|
||||||
playTimeInMilliseconds,
|
|
||||||
lastTimePlayed,
|
|
||||||
} = response.data;
|
|
||||||
|
|
||||||
gameRepository.update(
|
|
||||||
{ objectID },
|
|
||||||
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ const removeGameFromLibrary = async (
|
|||||||
const removeRemoveGameFromLibrary = async (gameId: number) => {
|
const removeRemoveGameFromLibrary = async (gameId: number) => {
|
||||||
const game = await gameRepository.findOne({ where: { id: gameId } });
|
const game = await gameRepository.findOne({ where: { id: gameId } });
|
||||||
|
|
||||||
if (game?.remoteId) {
|
if (game?.remoteId && HydraApi.isLoggedIn()) {
|
||||||
HydraApi.delete(`/games/${game.remoteId}`);
|
HydraApi.delete(`/games/${game.remoteId}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { logger } from "@main/services";
|
|||||||
const getMe = async (
|
const getMe = async (
|
||||||
_event: Electron.IpcMainInvokeEvent
|
_event: Electron.IpcMainInvokeEvent
|
||||||
): Promise<UserProfile | null> => {
|
): Promise<UserProfile | null> => {
|
||||||
|
if (!HydraApi.isLoggedIn()) return null;
|
||||||
|
|
||||||
return HydraApi.get(`/profile/me`)
|
return HydraApi.get(`/profile/me`)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
const me = response.data;
|
const me = response.data;
|
||||||
|
@ -95,18 +95,7 @@ const startGameDownload = async (
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
createGame(updatedGame!).then((response) => {
|
createGame(updatedGame!);
|
||||||
const {
|
|
||||||
id: remoteId,
|
|
||||||
playTimeInMilliseconds,
|
|
||||||
lastTimePlayed,
|
|
||||||
} = response.data;
|
|
||||||
|
|
||||||
gameRepository.update(
|
|
||||||
{ objectID },
|
|
||||||
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
await downloadQueueRepository.delete({ game: { id: updatedGame!.id } });
|
await downloadQueueRepository.delete({ game: { id: updatedGame!.id } });
|
||||||
await downloadQueueRepository.insert({ game: { id: updatedGame!.id } });
|
await downloadQueueRepository.insert({ game: { id: updatedGame!.id } });
|
||||||
|
@ -9,6 +9,8 @@ const getUser = async (
|
|||||||
_event: Electron.IpcMainInvokeEvent,
|
_event: Electron.IpcMainInvokeEvent,
|
||||||
userId: string
|
userId: string
|
||||||
): Promise<UserProfile | null> => {
|
): Promise<UserProfile | null> => {
|
||||||
|
if (!HydraApi.isLoggedIn()) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await HydraApi.get(`/user/${userId}`);
|
const response = await HydraApi.get(`/user/${userId}`);
|
||||||
const profile = response.data;
|
const profile = response.data;
|
||||||
|
@ -127,14 +127,12 @@ export class HydraApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static async revalidateAccessTokenIfExpired() {
|
private static async revalidateAccessTokenIfExpired() {
|
||||||
if (!this.userAuth.authToken) {
|
if (!this.isLoggedIn()) {
|
||||||
userAuthRepository.delete({ id: 1 });
|
|
||||||
logger.error("user is not logged in");
|
|
||||||
this.sendSignOutEvent();
|
|
||||||
throw new Error("user is not logged in");
|
throw new Error("user is not logged in");
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
||||||
try {
|
try {
|
||||||
const response = await this.instance.post(`/auth/refresh`, {
|
const response = await this.instance.post(`/auth/refresh`, {
|
||||||
|
@ -1,11 +1,25 @@
|
|||||||
import { Game } from "@main/entity";
|
import { Game } from "@main/entity";
|
||||||
import { HydraApi } from "../hydra-api";
|
import { HydraApi } from "../hydra-api";
|
||||||
|
import { gameRepository } from "@main/repository";
|
||||||
|
|
||||||
export const createGame = async (game: Game) => {
|
export const createGame = async (game: Game) => {
|
||||||
return HydraApi.post(`/games`, {
|
if (!HydraApi.isLoggedIn()) return;
|
||||||
|
|
||||||
|
HydraApi.post(`/games`, {
|
||||||
objectId: game.objectID,
|
objectId: game.objectID,
|
||||||
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds),
|
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds),
|
||||||
shop: game.shop,
|
shop: game.shop,
|
||||||
lastTimePlayed: game.lastTimePlayed,
|
lastTimePlayed: game.lastTimePlayed,
|
||||||
|
}).then((response) => {
|
||||||
|
const {
|
||||||
|
id: remoteId,
|
||||||
|
playTimeInMilliseconds,
|
||||||
|
lastTimePlayed,
|
||||||
|
} = response.data;
|
||||||
|
|
||||||
|
gameRepository.update(
|
||||||
|
{ objectID: game.objectID },
|
||||||
|
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
|
||||||
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,9 @@ export const updateGamePlaytime = async (
|
|||||||
deltaInMillis: number,
|
deltaInMillis: number,
|
||||||
lastTimePlayed: Date
|
lastTimePlayed: Date
|
||||||
) => {
|
) => {
|
||||||
return HydraApi.put(`/games/${game.remoteId}`, {
|
if (!HydraApi.isLoggedIn()) return;
|
||||||
|
|
||||||
|
HydraApi.put(`/games/${game.remoteId}`, {
|
||||||
playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000),
|
playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000),
|
||||||
lastTimePlayed,
|
lastTimePlayed,
|
||||||
});
|
});
|
||||||
|
@ -60,12 +60,7 @@ export const watchProcesses = async () => {
|
|||||||
if (game.remoteId) {
|
if (game.remoteId) {
|
||||||
updateGamePlaytime(game, 0, new Date());
|
updateGamePlaytime(game, 0, new Date());
|
||||||
} else {
|
} else {
|
||||||
createGame({ ...game, lastTimePlayed: new Date() }).then(
|
createGame({ ...game, lastTimePlayed: new Date() });
|
||||||
(response) => {
|
|
||||||
const { id: remoteId } = response.data;
|
|
||||||
gameRepository.update({ objectID: game.objectID }, { remoteId });
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gamesPlaytime.set(game.id, {
|
gamesPlaytime.set(game.id, {
|
||||||
@ -84,10 +79,7 @@ export const watchProcesses = async () => {
|
|||||||
game.lastTimePlayed!
|
game.lastTimePlayed!
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
createGame(game).then((response) => {
|
createGame(game);
|
||||||
const { id: remoteId } = response.data;
|
|
||||||
gameRepository.update({ objectID: game.objectID }, { remoteId });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,6 +98,8 @@ export function App() {
|
|||||||
fetchUserDetails().then((response) => {
|
fetchUserDetails().then((response) => {
|
||||||
if (response) updateUserDetails(response);
|
if (response) updateUserDetails(response);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
clearUserDetails();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [fetchUserDetails, updateUserDetails, dispatch]);
|
}, [fetchUserDetails, updateUserDetails, dispatch]);
|
||||||
|
@ -57,8 +57,14 @@ export function useUserDetails() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const fetchUserDetails = useCallback(async () => {
|
const fetchUserDetails = useCallback(async () => {
|
||||||
return window.electron.getMe();
|
return window.electron.getMe().then((userDetails) => {
|
||||||
}, []);
|
if (userDetails == null) {
|
||||||
|
clearUserDetails();
|
||||||
|
}
|
||||||
|
|
||||||
|
return userDetails;
|
||||||
|
});
|
||||||
|
}, [clearUserDetails]);
|
||||||
|
|
||||||
const patchUser = useCallback(
|
const patchUser = useCallback(
|
||||||
async (displayName: string, imageProfileUrl: string | null) => {
|
async (displayName: string, imageProfileUrl: string | null) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user