Correction mise à jour jeton authentification
This commit is contained in:
parent
38117ade07
commit
246dae446f
@ -1,6 +1,7 @@
|
|||||||
import { useAuth } from '@/hooks/useAuth'
|
import { useAuth } from '@/hooks/useAuth'
|
||||||
import { useGame, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
import { useGame, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
||||||
import { useDownloadTrains } from '@/hooks/useTrain'
|
import { useDownloadTrains } from '@/hooks/useTrain'
|
||||||
|
import { isAuthValid } from '@/utils/features/auth/authSlice'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { ReactNode, useEffect } from 'react'
|
import { ReactNode, useEffect } from 'react'
|
||||||
|
|
||||||
@ -12,11 +13,11 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
|||||||
const downloadTrains = useDownloadTrains()
|
const downloadTrains = useDownloadTrains()
|
||||||
|
|
||||||
const gameQuery = useQuery({
|
const gameQuery = useQuery({
|
||||||
queryKey: ['get-game'],
|
queryKey: ['get-game', auth.token],
|
||||||
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/`, {
|
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/`, {
|
||||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||||
).then(resp => resp.json()),
|
).then(resp => resp.json()),
|
||||||
enabled: auth.loggedIn,
|
enabled: isAuthValid(auth),
|
||||||
refetchInterval: 5000,
|
refetchInterval: 5000,
|
||||||
})
|
})
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -25,11 +26,11 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
|||||||
}, [gameQuery.status, gameQuery.dataUpdatedAt])
|
}, [gameQuery.status, gameQuery.dataUpdatedAt])
|
||||||
|
|
||||||
const playerQuery = useQuery({
|
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}/`, {
|
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/players/${game.playerId}/`, {
|
||||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||||
).then(resp => resp.json()),
|
).then(resp => resp.json()),
|
||||||
enabled: auth.loggedIn && !!game.playerId,
|
enabled: isAuthValid(auth) && !!game.playerId,
|
||||||
refetchInterval: 5000,
|
refetchInterval: 5000,
|
||||||
})
|
})
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -38,15 +39,15 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
|||||||
}, [playerQuery.status, playerQuery.dataUpdatedAt])
|
}, [playerQuery.status, playerQuery.dataUpdatedAt])
|
||||||
|
|
||||||
const trainsQuery = useQuery({
|
const trainsQuery = useQuery({
|
||||||
queryKey: ['get-trains'],
|
queryKey: ['get-trains', auth.token],
|
||||||
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/trains/`, {
|
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/trains/`, {
|
||||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||||
).then(resp => resp.json()),
|
).then(resp => resp.json()),
|
||||||
enabled: auth.loggedIn,
|
enabled: isAuthValid(auth),
|
||||||
refetchInterval: 5000,
|
refetchInterval: 5000,
|
||||||
})
|
})
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (trainsQuery.isSuccess && trainsQuery.data)
|
if (trainsQuery.isSuccess && trainsQuery.data && trainsQuery)
|
||||||
downloadTrains(trainsQuery.data)
|
downloadTrains(trainsQuery.data)
|
||||||
}, [trainsQuery.status, trainsQuery.dataUpdatedAt])
|
}, [trainsQuery.status, trainsQuery.dataUpdatedAt])
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ export const useGameStartMutation = ({ auth, updateGameState, onPostSuccess, onE
|
|||||||
}).then(resp => resp.json())
|
}).then(resp => resp.json())
|
||||||
},
|
},
|
||||||
onSuccess: async (data) => {
|
onSuccess: async (data) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
@ -59,7 +59,7 @@ export const useGameStopMutation = ({ auth, updateGameState, onPostSuccess, onEr
|
|||||||
}).then(resp => resp.json())
|
}).then(resp => resp.json())
|
||||||
},
|
},
|
||||||
onSuccess: async (data) => {
|
onSuccess: async (data) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
@ -87,7 +87,7 @@ export const useGameSwitchPlayerMutation = ({ auth, updateGameState, onPostSucce
|
|||||||
}).then(resp => resp.json())
|
}).then(resp => resp.json())
|
||||||
},
|
},
|
||||||
onSuccess: async (data) => {
|
onSuccess: async (data) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
@ -115,7 +115,7 @@ export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: GameProp
|
|||||||
}).then(resp => resp.json())
|
}).then(resp => resp.json())
|
||||||
},
|
},
|
||||||
onSuccess: async (data) => {
|
onSuccess: async (data) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
@ -142,7 +142,7 @@ export const useGameResetMutation = ({ auth, updateGameState, onPostSuccess, onE
|
|||||||
}).then(resp => resp.json())
|
}).then(resp => resp.json())
|
||||||
},
|
},
|
||||||
onSuccess: async (data) => {
|
onSuccess: async (data) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
|
@ -33,7 +33,7 @@ export const useLoginMutation = ({ authLogin, onPostSuccess, onError }: LoginPro
|
|||||||
},
|
},
|
||||||
networkMode: 'always',
|
networkMode: 'always',
|
||||||
onSuccess: async (data, { name, password }: LoginForm) => {
|
onSuccess: async (data, { name, password }: LoginForm) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
|
@ -33,7 +33,7 @@ export const useAddTrainMutation = ({ auth, onPostSuccess, onError }: TrainProps
|
|||||||
}).then(resp => resp.json())
|
}).then(resp => resp.json())
|
||||||
},
|
},
|
||||||
onSuccess: async (data) => {
|
onSuccess: async (data) => {
|
||||||
if (data.error) {
|
if (data.statusCode) {
|
||||||
if (onError)
|
if (onError)
|
||||||
onError({ response: data })
|
onError({ response: data })
|
||||||
return
|
return
|
||||||
|
@ -20,6 +20,16 @@ const initialState: AuthState = {
|
|||||||
token: null,
|
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({
|
export const authSlice = createSlice({
|
||||||
name: 'auth',
|
name: 'auth',
|
||||||
initialState: initialState,
|
initialState: initialState,
|
||||||
|
@ -92,6 +92,7 @@ export const trainSlice = createSlice({
|
|||||||
initialState: initialState,
|
initialState: initialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
downloadTrains(state, action: PayloadAction<TrainsPayload>) {
|
downloadTrains(state, action: PayloadAction<TrainsPayload>) {
|
||||||
|
if (state.trains)
|
||||||
state.trains = state.trains.filter(train => action.payload.data.filter(dlTrain => dlTrain.id === train.id) === null)
|
state.trains = state.trains.filter(train => action.payload.data.filter(dlTrain => dlTrain.id === train.id) === null)
|
||||||
for (const dlTrain of action.payload.data) {
|
for (const dlTrain of action.payload.data) {
|
||||||
const info = dlTrain.infoJson ? JSON.parse(dlTrain.infoJson) : undefined
|
const info = dlTrain.infoJson ? JSON.parse(dlTrain.infoJson) : undefined
|
||||||
|
Loading…
Reference in New Issue
Block a user