UserData
The UserData interface defines the structure for basic user profile information stored in Firestore, typically associated with an authenticated user.
Properties
display_name:stringThe user’s display name.create_time:anyThe timestamp when the user account was created. This can be a FirebaseTimestampobject or a compatible type.email:stringThe user’s email address.avatar_url:string | nullThe URL to the user’s avatar image, ornullif no avatar is set.
Usage
The UserData interface is used to type user profile documents stored in a Firestore collection (e.g., users/{uid}). Components like Avatar and layouts like AuthenticatedLayout consume this data to display user information.
// Example of a Firestore document for a user
interface FirestoreUserDocument {
display_name: "John Doe";
create_time: {
_seconds: 1678886400,
_nanoseconds: 0
};
email: "john.doe@example.com";
avatar_url: "https://example.com/avatars/john.jpg";
}
// In a React component or hook:
import { useAuth } from '@fireact.dev/app';
import { doc, getDoc } from 'firebase/firestore';
import { type UserData } from '@fireact.dev/app/types'; // Import the type
function UserProfileDisplay() {
const { currentUser } = useAuth();
const [userData, setUserData] = useState<UserData | null>(null);
useEffect(() => {
async function fetchProfile() {
if (currentUser) {
const userDocRef = doc(firestoreInstance, 'users', currentUser.uid);
const userDocSnap = await getDoc(userDocRef);
if (userDocSnap.exists()) {
setUserData(userDocSnap.data() as UserData);
}
}
}
fetchProfile();
}, [currentUser]);
return (
<div>
{userData ? (
<>
<p>Name: {userData.display_name}</p>
<p>Email: {userData.email}</p>
{userData.avatar_url && <img src={userData.avatar_url} alt="Avatar" />}
</>
) : (
<p>Loading user data...</p>
)}
</div>
);
}
Related Components/Hooks
Avatarcomponent: Displays a user’s avatar usingUserData.AuthenticatedLayoutcomponent: Fetches and displaysUserDatain the navigation bar.Profilecomponent: Allows users to view and edit theirUserData.EditNamecomponent: Modifies thedisplay_nameproperty ofUserData.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.