diff --git a/app/league/all/page.tsx b/app/league/all/page.tsx index 03f9e1b1..b6eabdcd 100644 --- a/app/league/all/page.tsx +++ b/app/league/all/page.tsx @@ -9,6 +9,7 @@ import { IGameWeek, ILeague } from '@/api/apiFunctions.interface'; import { getUserLeagues } from '@/utils/utils'; import { useDataStore } from '@/store/dataStore'; import { getGameWeek } from '@/api/apiFunctions'; +import { useAuthContext } from '@/context/AuthContextProvider'; /** * Renders the leagues component. @@ -18,6 +19,11 @@ const Leagues = (): JSX.Element => { const [leagues, setLeagues] = useState([]); const [currentWeek, setCurrentWeek] = useState(1); const { user } = useDataStore((state) => state); + const { getUser } = useAuthContext(); + + if (!user.id || user.id === '') { + getUser(); + } /** * Fetches the user's leagues. diff --git a/context/AuthContextProvider.tsx b/context/AuthContextProvider.tsx index 92ea24df..d210368e 100644 --- a/context/AuthContextProvider.tsx +++ b/context/AuthContextProvider.tsx @@ -2,8 +2,8 @@ // Licensed under the MIT License. 'use client'; -import React, { JSX, useCallback } from 'react'; -import { createContext, useContext, useEffect, useMemo, useState } from 'react'; +import React, { JSX } from 'react'; +import { createContext, useContext, useMemo, useState } from 'react'; import { account } from '@/api/config'; import { useRouter } from 'next/navigation'; import { useDataStore } from '@/store/dataStore'; @@ -21,7 +21,7 @@ type AuthContextType = { setIsSignedIn: React.Dispatch>; loginAccount: (user: UserCredentials) => Promise; // eslint-disable-line no-unused-vars logoutAccount: () => Promise; - getUser: () => Promise; + getUser: () => Promise; }; export const AuthContext = createContext(null); @@ -38,19 +38,9 @@ export const AuthContextProvider = ({ children: React.ReactNode; }): JSX.Element => { const [isSignedIn, setIsSignedIn] = useState(false); - const { updateUser, resetUser, user } = useDataStore( - (state) => state, - ); + const { updateUser, resetUser } = useDataStore((state) => state); const router = useRouter(); - useEffect(() => { - if (user.id === '' || user.email === '') { - getUser(); - return; - } - setIsSignedIn(true); - }, [user]); - /** * Authenticate and set session state * @param user - The user credentials. @@ -59,7 +49,6 @@ export const AuthContextProvider = ({ const loginAccount = async (user: UserCredentials): Promise => { try { await account.createEmailPasswordSession(user.email, user.password); - await getUser(); // Fetch user data and update state router.push('/league/all'); } catch (error) { console.error('Login error:', error); @@ -84,9 +73,9 @@ export const AuthContextProvider = ({ /** * Get user data from the session - * @returns {Promise} + * @returns {Promise} - The user data or an error. */ - const getUser = useCallback(async () => { + const getUser = async (): Promise => { if (!isSessionInLocalStorage()) { router.push('/login'); return; @@ -101,7 +90,7 @@ export const AuthContextProvider = ({ resetUser(); setIsSignedIn(false); } - }, [user]); + }; /** * Helper function to validate session data in local storage