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}