From db560c401a48cbe8993e3eb094bf0a7b2e5a96c7 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Wed, 11 Dec 2024 21:43:44 +0100 Subject: [PATCH] =?UTF-8?q?R=C3=A9cup=C3=A9ration=20de=20l'identifiant=20d?= =?UTF-8?q?e=20l=C3=A6=20joueur=E2=8B=85se?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/components/LoginProvider.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/client/components/LoginProvider.tsx b/client/components/LoginProvider.tsx index 336b9ec..ee61d42 100644 --- a/client/components/LoginProvider.tsx +++ b/client/components/LoginProvider.tsx @@ -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}