diff --git a/client/components/GameProvider.tsx b/client/components/GameProvider.tsx index 605b795..7a95079 100644 --- a/client/components/GameProvider.tsx +++ b/client/components/GameProvider.tsx @@ -1,6 +1,7 @@ import { useAuth } from '@/hooks/useAuth' import { useGame, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame' import { useDownloadTrains } from '@/hooks/useTrain' +import { isAuthValid } from '@/utils/features/auth/authSlice' import { useQuery } from '@tanstack/react-query' import { ReactNode, useEffect } from 'react' @@ -12,11 +13,11 @@ export default function GameProvider({ children }: { children: ReactNode }) { const downloadTrains = useDownloadTrains() const gameQuery = useQuery({ - queryKey: ['get-game'], + queryKey: ['get-game', auth.token], queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/`, { headers: { "Authorization": `Bearer ${auth.token}` }} ).then(resp => resp.json()), - enabled: auth.loggedIn, + enabled: isAuthValid(auth), refetchInterval: 5000, }) useEffect(() => { @@ -25,11 +26,11 @@ export default function GameProvider({ children }: { children: ReactNode }) { }, [gameQuery.status, gameQuery.dataUpdatedAt]) const playerQuery = useQuery({ - queryKey: ['get-player', game.playerId], + queryKey: ['get-player', game.playerId, auth.token], queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/players/${game.playerId}/`, { headers: { "Authorization": `Bearer ${auth.token}` }} ).then(resp => resp.json()), - enabled: auth.loggedIn && !!game.playerId, + enabled: isAuthValid(auth) && !!game.playerId, refetchInterval: 5000, }) useEffect(() => { @@ -38,15 +39,15 @@ export default function GameProvider({ children }: { children: ReactNode }) { }, [playerQuery.status, playerQuery.dataUpdatedAt]) const trainsQuery = useQuery({ - queryKey: ['get-trains'], + queryKey: ['get-trains', auth.token], queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/trains/`, { headers: { "Authorization": `Bearer ${auth.token}` }} ).then(resp => resp.json()), - enabled: auth.loggedIn, + enabled: isAuthValid(auth), refetchInterval: 5000, }) useEffect(() => { - if (trainsQuery.isSuccess && trainsQuery.data) + if (trainsQuery.isSuccess && trainsQuery.data && trainsQuery) downloadTrains(trainsQuery.data) }, [trainsQuery.status, trainsQuery.dataUpdatedAt]) diff --git a/client/hooks/mutations/useGameMutation.ts b/client/hooks/mutations/useGameMutation.ts index e423f1a..f622515 100644 --- a/client/hooks/mutations/useGameMutation.ts +++ b/client/hooks/mutations/useGameMutation.ts @@ -31,7 +31,7 @@ export const useGameStartMutation = ({ auth, updateGameState, onPostSuccess, onE }).then(resp => resp.json()) }, onSuccess: async (data) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return @@ -59,7 +59,7 @@ export const useGameStopMutation = ({ auth, updateGameState, onPostSuccess, onEr }).then(resp => resp.json()) }, onSuccess: async (data) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return @@ -87,7 +87,7 @@ export const useGameSwitchPlayerMutation = ({ auth, updateGameState, onPostSucce }).then(resp => resp.json()) }, onSuccess: async (data) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return @@ -115,7 +115,7 @@ export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: GameProp }).then(resp => resp.json()) }, onSuccess: async (data) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return @@ -142,7 +142,7 @@ export const useGameResetMutation = ({ auth, updateGameState, onPostSuccess, onE }).then(resp => resp.json()) }, onSuccess: async (data) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return diff --git a/client/hooks/mutations/useLoginMutation.ts b/client/hooks/mutations/useLoginMutation.ts index a35c87f..f5a2083 100644 --- a/client/hooks/mutations/useLoginMutation.ts +++ b/client/hooks/mutations/useLoginMutation.ts @@ -33,7 +33,7 @@ export const useLoginMutation = ({ authLogin, onPostSuccess, onError }: LoginPro }, networkMode: 'always', onSuccess: async (data, { name, password }: LoginForm) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return diff --git a/client/hooks/mutations/useTrainMutation.ts b/client/hooks/mutations/useTrainMutation.ts index fd08f1f..a18d367 100644 --- a/client/hooks/mutations/useTrainMutation.ts +++ b/client/hooks/mutations/useTrainMutation.ts @@ -33,7 +33,7 @@ export const useAddTrainMutation = ({ auth, onPostSuccess, onError }: TrainProps }).then(resp => resp.json()) }, onSuccess: async (data) => { - if (data.error) { + if (data.statusCode) { if (onError) onError({ response: data }) return diff --git a/client/utils/features/auth/authSlice.ts b/client/utils/features/auth/authSlice.ts index 7192b21..96bf706 100644 --- a/client/utils/features/auth/authSlice.ts +++ b/client/utils/features/auth/authSlice.ts @@ -20,6 +20,16 @@ const initialState: AuthState = { token: null, } +export function isAuthValid({ loggedIn, token }: AuthState): boolean { + if (!loggedIn || token === null) + return false + const arrayToken = token.split('.') + const tokenPayload = JSON.parse(atob(arrayToken[1])) + const expTime: number = tokenPayload.exp * 1000 + const now: number = Math.floor(new Date().getTime()) + return expTime >= now +} + export const authSlice = createSlice({ name: 'auth', initialState: initialState, diff --git a/client/utils/features/train/trainSlice.ts b/client/utils/features/train/trainSlice.ts index 15bb0a7..e29b11b 100644 --- a/client/utils/features/train/trainSlice.ts +++ b/client/utils/features/train/trainSlice.ts @@ -92,7 +92,8 @@ export const trainSlice = createSlice({ initialState: initialState, reducers: { downloadTrains(state, action: PayloadAction) { - state.trains = state.trains.filter(train => action.payload.data.filter(dlTrain => dlTrain.id === train.id) === null) + if (state.trains) + state.trains = state.trains.filter(train => action.payload.data.filter(dlTrain => dlTrain.id === train.id) === null) for (const dlTrain of action.payload.data) { const info = dlTrain.infoJson ? JSON.parse(dlTrain.infoJson) : undefined state.trains.push({