Récupération de l'identifiant de læ joueur⋅se
This commit is contained in:
parent
61b0cd51ae
commit
db560c401a
@ -4,6 +4,7 @@ import { ReactNode, useEffect } from 'react'
|
|||||||
import { useAuth, useAuthLogin } from '@/hooks/useAuth'
|
import { useAuth, useAuthLogin } from '@/hooks/useAuth'
|
||||||
import * as SecureStore from '@/utils/SecureStore'
|
import * as SecureStore from '@/utils/SecureStore'
|
||||||
import { useLoginMutation } from '@/hooks/mutations/useLoginMutation'
|
import { useLoginMutation } from '@/hooks/mutations/useLoginMutation'
|
||||||
|
import { useGame, useSetPlayerId } from '@/hooks/useGame'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
loginRedirect: Href
|
loginRedirect: Href
|
||||||
@ -24,6 +25,8 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
|
|||||||
authLogin({ name: auth.name ?? "", token: null })
|
authLogin({ name: auth.name ?? "", token: null })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
const game = useGame()
|
||||||
|
const setPlayerId = useSetPlayerId()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
@ -35,7 +38,7 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Si on est pas connecté⋅e, on reste sur la fenêtre de connexion
|
// Si on est pas connecté⋅e, on reste sur la fenêtre de connexion
|
||||||
if ((!auth.loggedIn || !auth.token) && route.pathname !== loginRedirect)
|
if ((!auth.loggedIn || !auth.token) && route.pathname !== loginRedirect)
|
||||||
router.navigate(loginRedirect)
|
router.navigate(loginRedirect)
|
||||||
})()
|
})()
|
||||||
}, [auth, authLogin, router, route])
|
}, [auth, authLogin, router, route])
|
||||||
@ -53,6 +56,10 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
|
|||||||
const expTime: number = tokenPayload.exp * 1000
|
const expTime: number = tokenPayload.exp * 1000
|
||||||
const now: number = Math.floor(new Date().getTime())
|
const now: number = Math.floor(new Date().getTime())
|
||||||
waitTime = expTime - now
|
waitTime = expTime - now
|
||||||
|
|
||||||
|
const playerId = tokenPayload.playerId
|
||||||
|
if (playerId !== game.playerId)
|
||||||
|
setPlayerId(playerId)
|
||||||
}
|
}
|
||||||
const timeout = setTimeout(async () => {
|
const timeout = setTimeout(async () => {
|
||||||
const password = SecureStore.getItem('apiPassword')
|
const password = SecureStore.getItem('apiPassword')
|
||||||
@ -62,7 +69,7 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
|
|||||||
authLogin({ name: name, token: null })
|
authLogin({ name: name, token: null })
|
||||||
}, waitTime)
|
}, waitTime)
|
||||||
return () => clearTimeout(timeout)
|
return () => clearTimeout(timeout)
|
||||||
}, [auth, authLogin])
|
}, [auth, authLogin, game, setPlayerId])
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
{children}
|
{children}
|
||||||
|
Loading…
Reference in New Issue
Block a user