feat: prevent api calls when user is not logged in

This commit is contained in:
Zamitto 2024-07-01 15:48:52 -03:00
parent 9870213fff
commit dd23358a95
11 changed files with 39 additions and 43 deletions

View File

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

View File

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

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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`, {

View File

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

View File

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

View File

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

View File

@ -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]);

View File

@ -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) => {