useHeader Hook Logic - abhiram-shaji/Langroove GitHub Wiki
This documentation provides an overview of the useHeader
hook, which manages user authentication state and retrieves user information for display in a header or similar component.
The useHeader
hook:
- Tracks the current user's authentication state and user ID.
- Integrates with the
useUserInfo
hook to fetch user information based on the user ID. - Manages loading states for both authentication and user information retrieval.
- Listens to changes in Firebase Authentication state to retrieve the current user's ID.
-
Functionality:
- Uses Firebase Authentication's
onAuthStateChanged
to monitor login/logout events. - Updates the
userId
state with the logged-in user's UID ornull
if no user is authenticated.
- Uses Firebase Authentication's
-
Code Snippet:
useEffect(() => { const unsubscribe = auth.onAuthStateChanged((currentUser) => { if (currentUser) { setUserId(currentUser.uid); } else { setUserId(null); } setIsUserLoading(false); // Stop loading after user is fetched }); return () => unsubscribe(); }, []);
-
Process:
- Sets
userId
to the UID of the authenticated user ornull
if unauthenticated. - Updates
isUserLoading
tofalse
after processing.
- Sets
- Retrieves detailed user information (e.g., name, email) for display in the header.
-
Functionality:
- Invokes the
useUserInfo
hook with theuserId
to fetch associated user details.
- Invokes the
-
Code Snippet:
const { userInfo, loading: userInfoLoading } = useUserInfo(userId || '');
-
Process:
- Passes the
userId
touseUserInfo
only if it's non-empty. - Tracks the loading state and retrieved
userInfo
provided byuseUserInfo
.
- Passes the
- Differentiates between the loading state for authentication and user information retrieval.
-
State Variables:
-
isUserLoading
: Tracks whether authentication state is being processed. -
userInfoLoading
: Tracks whether user information is being fetched.
-
-
Combined Usage:
-
isUserLoading
is set tofalse
onceonAuthStateChanged
finishes. -
userInfoLoading
reflects the loading state of theuseUserInfo
hook.
-
Property/Method | Description |
---|---|
userId |
The currently authenticated user's UID or null if unauthenticated. |
isUserLoading |
Boolean indicating whether the authentication state is being processed. |
userInfo |
Detailed user information fetched using the useUserInfo hook. |
userInfoLoading |
Boolean indicating whether user information is being fetched. |
import React from 'react';
import useHeader from './hooks/useHeader';
const Header = () => {
const { userId, isUserLoading, userInfo, userInfoLoading } = useHeader();
if (isUserLoading || userInfoLoading) {
return <div>Loading...</div>;
}
return (
<header>
{userId && userInfo ? (
<div>
<h1>Welcome, {userInfo.name}</h1>
<p>Email: {userInfo.email}</p>
</div>
) : (
<div>
<h1>Welcome, Guest</h1>
</div>
)}
</header>
);
};
export default Header;
-
Unauthenticated State:
- If no user is logged in,
userId
is set tonull
, anduserInfo
is not fetched.
- If no user is logged in,
-
Empty User Info:
- If
userInfo
is not available, the component can display a fallback message.
- If
- Relies on the
useUserInfo
hook to fetch user details. - Requires Firebase Authentication (
auth
) to monitor authentication state.
- Ensures real-time updates on authentication changes using Firebase's listener.
- Optimized by separating authentication and user info loading states.
For more information, refer to the Firebase Authentication Documentation and React Documentation on Hooks.