fix: crate or update user

This commit is contained in:
xavidop
2025-07-23 16:57:16 +02:00
parent f1edae23ce
commit c7f4ff9899
3 changed files with 24 additions and 2 deletions
+4 -1
View File
@@ -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
+6 -1
View File
@@ -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(),
};
+14
View File
@@ -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<T extends Record<string, any>>(obj: T): Partial<T> {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (value !== undefined) {
acc[key as keyof T] = value;
}
return acc;
}, {} as Partial<T>);
}