diff --git a/src/main/events/library/add-game-to-library.ts b/src/main/events/library/add-game-to-library.ts index 8187d41e..1bda0c93 100644 --- a/src/main/events/library/add-game-to-library.ts +++ b/src/main/events/library/add-game-to-library.ts @@ -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!); }); }; diff --git a/src/main/events/library/remove-game-from-library.ts b/src/main/events/library/remove-game-from-library.ts index d7874b8f..9139696b 100644 --- a/src/main/events/library/remove-game-from-library.ts +++ b/src/main/events/library/remove-game-from-library.ts @@ -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}`); } }; diff --git a/src/main/events/profile/get-me.ts b/src/main/events/profile/get-me.ts index 17eb3d38..e8aa72d8 100644 --- a/src/main/events/profile/get-me.ts +++ b/src/main/events/profile/get-me.ts @@ -8,6 +8,8 @@ import { logger } from "@main/services"; const getMe = async ( _event: Electron.IpcMainInvokeEvent ): Promise => { + if (!HydraApi.isLoggedIn()) return null; + return HydraApi.get(`/profile/me`) .then((response) => { const me = response.data; diff --git a/src/main/events/torrenting/start-game-download.ts b/src/main/events/torrenting/start-game-download.ts index 0aa01fc9..cea41596 100644 --- a/src/main/events/torrenting/start-game-download.ts +++ b/src/main/events/torrenting/start-game-download.ts @@ -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 } }); diff --git a/src/main/events/user/get-user.ts b/src/main/events/user/get-user.ts index 596df084..5b4dfae5 100644 --- a/src/main/events/user/get-user.ts +++ b/src/main/events/user/get-user.ts @@ -9,6 +9,8 @@ const getUser = async ( _event: Electron.IpcMainInvokeEvent, userId: string ): Promise => { + if (!HydraApi.isLoggedIn()) return null; + try { const response = await HydraApi.get(`/user/${userId}`); const profile = response.data; diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index fb8b9a28..505ea209 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -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`, { diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index 6e56a3de..8f935bef 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -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 } + ); }); }; diff --git a/src/main/services/library-sync/update-game-playtime.ts b/src/main/services/library-sync/update-game-playtime.ts index 190d7e7d..3a7cca22 100644 --- a/src/main/services/library-sync/update-game-playtime.ts +++ b/src/main/services/library-sync/update-game-playtime.ts @@ -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, }); diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index f20b4128..da7b909d 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -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); } } } diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index 0f9d3972..27fa2646 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -98,6 +98,8 @@ export function App() { fetchUserDetails().then((response) => { if (response) updateUserDetails(response); }); + } else { + clearUserDetails(); } }); }, [fetchUserDetails, updateUserDetails, dispatch]); diff --git a/src/renderer/src/hooks/use-user-details.ts b/src/renderer/src/hooks/use-user-details.ts index 1d8257f4..e87f8ff6 100644 --- a/src/renderer/src/hooks/use-user-details.ts +++ b/src/renderer/src/hooks/use-user-details.ts @@ -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) => {