import React, { useState } from 'react'; import { doc, updateDoc, deleteDoc } from 'firebase/firestore'; import { updatePassword, updateProfile, deleteUser, EmailAuthProvider, reauthenticateWithCredential } from 'firebase/auth'; import { ref, listAll, deleteObject } from 'firebase/storage'; import { auth, db, storage } from '../lib/firebase'; import { Loader2, User, Mail, Save, AlertTriangle, ShieldCheck } from 'lucide-react'; import { motion } from 'motion/react'; export const ProfileView: React.FC<{ user: any, userData: any, onUpdate: (data: any) => void, onDeleted: () => void }> = ({ user, userData, onUpdate, onDeleted }) => { const [name, setName] = useState(userData?.name || user?.displayName || ''); const [newPassword, setNewPassword] = useState(''); const [currentPassword, setCurrentPassword] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [messages, setMessages] = useState<{ error?: string, success?: string }>({}); const handleUpdate = async (e: React.FormEvent) => { e.preventDefault(); if (!auth.currentUser) return; setIsSubmitting(true); setMessages({}); try { if (name !== user.displayName) { await updateProfile(auth.currentUser, { displayName: name }); await updateDoc(doc(db, 'users', auth.currentUser.uid), { name }); onUpdate({ ...userData, name }); } if (newPassword && currentPassword) { const credential = EmailAuthProvider.credential(auth.currentUser.email!, currentPassword); await reauthenticateWithCredential(auth.currentUser, credential); await updatePassword(auth.currentUser, newPassword); } else if (newPassword && !currentPassword) { throw new Error('Please enter your current password to set a new one.'); } setMessages({ success: 'Profile updated successfully!' }); setNewPassword(''); setCurrentPassword(''); } catch (err: any) { console.error(err); setMessages({ error: err.message || 'Failed to update profile.' }); } finally { setIsSubmitting(false); } }; const handleDelete = async () => { if (!auth.currentUser || !currentPassword) { setMessages({ error: 'Please enter your current password to delete your account.' }); return; } if (!window.confirm('Are you sure you want to delete your account? This action cannot be undone.')) return; setIsDeleting(true); setMessages({}); try { const credential = EmailAuthProvider.credential(auth.currentUser.email!, currentPassword); await reauthenticateWithCredential(auth.currentUser, credential); const uid = auth.currentUser.uid; // Delete storage folder try { const folderRef = ref(storage, `user_uploads/${uid}`); const listRes = await listAll(folderRef); const deletePromises = listRes.items.map((itemRef) => deleteObject(itemRef)); await Promise.all(deletePromises); } catch (storageErr) { console.error('Error deleting user storage files:', storageErr); } // Delete document from firestore first await deleteDoc(doc(db, 'users', uid)); // Then delete from auth await deleteUser(auth.currentUser); onDeleted(); } catch (err: any) { console.error(err); setMessages({ error: err.message || 'Failed to delete account. Make sure your password is correct.' }); setIsDeleting(false); } }; return (
{user?.email}