Récupération de l'identifiant de læ joueur⋅se

This commit is contained in:
Emmy D'Anello 2024-12-11 21:43:44 +01:00
parent 61b0cd51ae
commit db560c401a
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85

View File

@ -4,6 +4,7 @@ import { ReactNode, useEffect } from 'react'
import { useAuth, useAuthLogin } from '@/hooks/useAuth'
import * as SecureStore from '@/utils/SecureStore'
import { useLoginMutation } from '@/hooks/mutations/useLoginMutation'
import { useGame, useSetPlayerId } from '@/hooks/useGame'
type Props = {
loginRedirect: Href
@ -24,6 +25,8 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
authLogin({ name: auth.name ?? "", token: null })
}
})
const game = useGame()
const setPlayerId = useSetPlayerId()
useEffect(() => {
(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
if ((!auth.loggedIn || !auth.token) && route.pathname !== loginRedirect)
if ((!auth.loggedIn || !auth.token) && route.pathname !== loginRedirect)
router.navigate(loginRedirect)
})()
}, [auth, authLogin, router, route])
@ -53,6 +56,10 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
const expTime: number = tokenPayload.exp * 1000
const now: number = Math.floor(new Date().getTime())
waitTime = expTime - now
const playerId = tokenPayload.playerId
if (playerId !== game.playerId)
setPlayerId(playerId)
}
const timeout = setTimeout(async () => {
const password = SecureStore.getItem('apiPassword')
@ -62,7 +69,7 @@ export default function LoginProvider({ loginRedirect, children }: Props) {
authLogin({ name: name, token: null })
}, waitTime)
return () => clearTimeout(timeout)
}, [auth, authLogin])
}, [auth, authLogin, game, setPlayerId])
return <>
{children}