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
useUserInfohook 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
onAuthStateChangedto monitor login/logout events. - Updates the
userIdstate with the logged-in user's UID ornullif 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
userIdto the UID of the authenticated user ornullif unauthenticated. - Updates
isUserLoadingtofalseafter processing.
- Sets
- Retrieves detailed user information (e.g., name, email) for display in the header.
-
Functionality:
- Invokes the
useUserInfohook with theuserIdto fetch associated user details.
- Invokes the
-
Code Snippet:
const { userInfo, loading: userInfoLoading } = useUserInfo(userId || '');
-
Process:
- Passes the
userIdtouseUserInfoonly if it's non-empty. - Tracks the loading state and retrieved
userInfoprovided 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:
-
isUserLoadingis set tofalseonceonAuthStateChangedfinishes. -
userInfoLoadingreflects the loading state of theuseUserInfohook.
-
| 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,
userIdis set tonull, anduserInfois not fetched.
- If no user is logged in,
-
Empty User Info:
- If
userInfois not available, the component can display a fallback message.
- If
- Relies on the
useUserInfohook 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.