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 } });
createGame(game!).then((response) => {
const {
id: remoteId,
playTimeInMilliseconds,
lastTimePlayed,
} = response.data;
gameRepository.update(
{ objectID },
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
);
});
createGame(game!);
});
};

View File

@ -19,7 +19,7 @@ const removeGameFromLibrary = async (
const removeRemoveGameFromLibrary = async (gameId: number) => {
const game = await gameRepository.findOne({ where: { id: gameId } });
if (game?.remoteId) {
if (game?.remoteId && HydraApi.isLoggedIn()) {
HydraApi.delete(`/games/${game.remoteId}`);
}
};

View File

@ -8,6 +8,8 @@ import { logger } from "@main/services";
const getMe = async (
_event: Electron.IpcMainInvokeEvent
): Promise<UserProfile | null> => {
if (!HydraApi.isLoggedIn()) return null;
return HydraApi.get(`/profile/me`)
.then((response) => {
const me = response.data;

View File

@ -95,18 +95,7 @@ const startGameDownload = async (
},
});
createGame(updatedGame!).then((response) => {
const {
id: remoteId,
playTimeInMilliseconds,
lastTimePlayed,
} = response.data;
gameRepository.update(
{ objectID },
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
);
});
createGame(updatedGame!);
await downloadQueueRepository.delete({ game: { id: updatedGame!.id } });
await downloadQueueRepository.insert({ game: { id: updatedGame!.id } });

View File

@ -9,6 +9,8 @@ const getUser = async (
_event: Electron.IpcMainInvokeEvent,
userId: string
): Promise<UserProfile | null> => {
if (!HydraApi.isLoggedIn()) return null;
try {
const response = await HydraApi.get(`/user/${userId}`);
const profile = response.data;

View File

@ -127,14 +127,12 @@ export class HydraApi {
}
private static async revalidateAccessTokenIfExpired() {
if (!this.userAuth.authToken) {
userAuthRepository.delete({ id: 1 });
logger.error("user is not logged in");
this.sendSignOutEvent();
if (!this.isLoggedIn()) {
throw new Error("user is not logged in");
}
const now = new Date();
if (this.userAuth.expirationTimestamp < now.getTime()) {
try {
const response = await this.instance.post(`/auth/refresh`, {

View File

@ -1,11 +1,25 @@
import { Game } from "@main/entity";
import { HydraApi } from "../hydra-api";
import { gameRepository } from "@main/repository";
export const createGame = async (game: Game) => {
return HydraApi.post(`/games`, {
if (!HydraApi.isLoggedIn()) return;
HydraApi.post(`/games`, {
objectId: game.objectID,
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds),
shop: game.shop,
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,
lastTimePlayed: Date
) => {
return HydraApi.put(`/games/${game.remoteId}`, {
if (!HydraApi.isLoggedIn()) return;
HydraApi.put(`/games/${game.remoteId}`, {
playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000),
lastTimePlayed,
});

View File

@ -60,12 +60,7 @@ export const watchProcesses = async () => {
if (game.remoteId) {
updateGamePlaytime(game, 0, new Date());
} else {
createGame({ ...game, lastTimePlayed: new Date() }).then(
(response) => {
const { id: remoteId } = response.data;
gameRepository.update({ objectID: game.objectID }, { remoteId });
}
);
createGame({ ...game, lastTimePlayed: new Date() });
}
gamesPlaytime.set(game.id, {
@ -84,10 +79,7 @@ export const watchProcesses = async () => {
game.lastTimePlayed!
);
} else {
createGame(game).then((response) => {
const { id: remoteId } = response.data;
gameRepository.update({ objectID: game.objectID }, { remoteId });
});
createGame(game);
}
}
}

View File

@ -98,6 +98,8 @@ export function App() {
fetchUserDetails().then((response) => {
if (response) updateUserDetails(response);
});
} else {
clearUserDetails();
}
});
}, [fetchUserDetails, updateUserDetails, dispatch]);

View File

@ -57,8 +57,14 @@ export function useUserDetails() {
);
const fetchUserDetails = useCallback(async () => {
return window.electron.getMe();
}, []);
return window.electron.getMe().then((userDetails) => {
if (userDetails == null) {
clearUserDetails();
}
return userDetails;
});
}, [clearUserDetails]);
const patchUser = useCallback(
async (displayName: string, imageProfileUrl: string | null) => {