Correction mise à jour jeton authentification
This commit is contained in:
parent
38117ade07
commit
246dae446f
@ -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])
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -92,7 +92,8 @@ export const trainSlice = createSlice({
|
||||
initialState: initialState,
|
||||
reducers: {
|
||||
downloadTrains(state, action: PayloadAction<TrainsPayload>) {
|
||||
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({
|
||||
|
Loading…
Reference in New Issue
Block a user