From c7f4ff9899ff6f51bcbf51e6461221d46c8f8cb4 Mon Sep 17 00:00:00 2001 From: xavidop Date: Wed, 23 Jul 2025 16:57:16 +0200 Subject: [PATCH] fix: crate or update user --- src/components/providers/AuthProvider.tsx | 5 ++++- src/lib/firestore.ts | 7 ++++++- src/lib/utils.ts | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/components/providers/AuthProvider.tsx b/src/components/providers/AuthProvider.tsx index 2b8823e..cac6abf 100644 --- a/src/components/providers/AuthProvider.tsx +++ b/src/components/providers/AuthProvider.tsx @@ -5,6 +5,7 @@ import { onAuthStateChanged, type User } from 'firebase/auth'; import { auth } from '@/lib/firebase'; import { Loader2 } from 'lucide-react'; import { createOrUpdateUserProfile } from '@/lib/firestore'; +import { filterUndefinedValues } from '@/lib/utils'; export interface AuthContextType { user: User | null; @@ -24,11 +25,13 @@ export function AuthProvider({ children }: { children: ReactNode }) { // Create or update user profile when user signs in if (currentUser) { try { - await createOrUpdateUserProfile(currentUser.uid, { + const profileData = filterUndefinedValues({ email: currentUser.email || '', displayName: currentUser.displayName || undefined, photoURL: currentUser.photoURL || undefined, }); + + await createOrUpdateUserProfile(currentUser.uid, profileData); } catch (error) { console.error('Error creating/updating user profile:', error); // Don't block the auth flow for profile creation errors diff --git a/src/lib/firestore.ts b/src/lib/firestore.ts index 05acfee..bda889c 100644 --- a/src/lib/firestore.ts +++ b/src/lib/firestore.ts @@ -18,6 +18,7 @@ import { db, auth, storage } from './firebase'; // Main Firebase config import type { PokemonCard, ScannedCardData, UserProfile, UserApiKeys } from '@/types'; import { uploadCardImageToStorage, uploadVideoToStorage } from '@/utils/storageUtils'; import { ref, deleteObject } from 'firebase/storage'; +import { filterUndefinedValues } from './utils'; import { generateCardVideo } from '@/ai/flows/generate-card-video'; const CARDS_COLLECTION = 'users'; // Top-level collection for users @@ -322,8 +323,12 @@ export const createOrUpdateUserProfile = async (userId: string, profileData: Par try { const userRef = doc(db, 'users', userId); + + // Filter out undefined values to prevent Firestore errors + const filteredProfileData = filterUndefinedValues(profileData); + const updateData: any = { - ...profileData, + ...filteredProfileData, id: userId, updatedAt: serverTimestamp(), }; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index bd0c391..cd51e91 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,3 +4,17 @@ import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +/** + * Removes undefined values from an object to prevent Firestore errors + * @param obj The object to filter + * @returns A new object with undefined values removed + */ +export function filterUndefinedValues>(obj: T): Partial { + return Object.entries(obj).reduce((acc, [key, value]) => { + if (value !== undefined) { + acc[key as keyof T] = value; + } + return acc; + }, {} as Partial); +}