Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/league/all/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,6 +19,11 @@ const Leagues = (): JSX.Element => {
const [leagues, setLeagues] = useState<ILeague[]>([]);
const [currentWeek, setCurrentWeek] = useState<IGameWeek['week']>(1);
const { user } = useDataStore((state) => state);
const { getUser } = useAuthContext();

if (!user.id || user.id === '') {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go into the AuthContextProvider. Otherwise, every page will need this code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Done.

getUser();
}

/**
* Fetches the user's leagues.
Expand Down
25 changes: 7 additions & 18 deletions context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +21,7 @@ type AuthContextType = {
setIsSignedIn: React.Dispatch<React.SetStateAction<boolean>>;
loginAccount: (user: UserCredentials) => Promise<void | Error>; // eslint-disable-line no-unused-vars
logoutAccount: () => Promise<void>;
getUser: () => Promise<IUser | undefined>;
getUser: () => Promise<void | IUser>;
};

export const AuthContext = createContext<AuthContextType | null>(null);
Expand All @@ -38,19 +38,9 @@ export const AuthContextProvider = ({
children: React.ReactNode;
}): JSX.Element => {
const [isSignedIn, setIsSignedIn] = useState<boolean>(false);
const { updateUser, resetUser, user } = useDataStore<DataStore>(
(state) => state,
);
const { updateUser, resetUser } = useDataStore<DataStore>((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.
Expand All @@ -59,7 +49,6 @@ export const AuthContextProvider = ({
const loginAccount = async (user: UserCredentials): Promise<void | Error> => {
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);
Expand All @@ -84,9 +73,9 @@ export const AuthContextProvider = ({

/**
* Get user data from the session
* @returns {Promise<void>}
* @returns {Promise<void | IUser>} - The user data or an error.
*/
const getUser = useCallback(async () => {
const getUser = async (): Promise<void | IUser> => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need unit tests for this file. I do not see a test file at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roger that. I will add one.

if (!isSessionInLocalStorage()) {
router.push('/login');
return;
Expand All @@ -101,7 +90,7 @@ export const AuthContextProvider = ({
resetUser();
setIsSignedIn(false);
}
}, [user]);
};

/**
* Helper function to validate session data in local storage
Expand Down