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 { 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}