Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
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
4 changes: 0 additions & 4 deletions app/league/all/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ const Leagues = (): JSX.Element => {
};

useEffect(() => {
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 entire empty user will be fixed with this change? I had to add it to 2 other pages because that's how it works for now.

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.

@shashilo I am confused. need more info. I fixed this to remove it from page.tsx and moved that into the useEffect of authContextProvider as that is the only place I could get it to work and the provider wraps around all pages.

return;
}

getLeagues();
getCurrentGameWeek();
}, [user]);
Expand Down
28 changes: 13 additions & 15 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, useEffect } 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 @@ -43,14 +43,6 @@ export const AuthContextProvider = ({
);
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 +51,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 +75,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 +92,7 @@ export const AuthContextProvider = ({
resetUser();
setIsSignedIn(false);
}
}, [user]);
};

/**
* Helper function to validate session data in local storage
Expand All @@ -118,6 +109,13 @@ export const AuthContextProvider = ({
return true;
};

useEffect(() => {
if (!user.id || user.id === '') {
getUser();
return;
}
}, [user]);

// Memoize context values to avoid unnecessary re-renders
const contextValue = useMemo(
() => ({
Expand Down