From b10729112e1e8b409a1239c5b6ef10149b46bd84 Mon Sep 17 00:00:00 2001 From: Aditya Bijalwan Date: Sun, 19 Oct 2025 19:56:07 +0530 Subject: [PATCH] fix: delete accoutn functionality --- src/contexts/AuthContext.jsx | 72 +++++++++++++++++++++++++++++++ src/pages/Settings.jsx | 13 ++++-- src/services/userRecipeService.js | 20 +++++++++ 3 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/contexts/AuthContext.jsx b/src/contexts/AuthContext.jsx index d4011d6..e30d9bb 100644 --- a/src/contexts/AuthContext.jsx +++ b/src/contexts/AuthContext.jsx @@ -1,6 +1,7 @@ import React, { createContext, useContext, useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { account, databases } from '../appwriteClient'; +import { userRecipeService } from '../services/userRecipeService'; import { Query } from 'appwrite'; import { mealLoggingService } from '../services/mealLoggingService'; import { validatePassword } from '../utils/validation'; @@ -277,6 +278,75 @@ export const AuthProvider = ({ children }) => { throw error; } }; + + const deleteAccount = async () => { + try { + if (!user) return; + const userId = user.$id; + + // Best-effort cleanup of user data + try { + await userRecipeService.clearMealLogs(userId); + } catch (e) { + console.warn('Failed to clear meal logs during account deletion:', e); + } + try { + await userRecipeService.clearFavorites(userId); + } catch (e) { + console.warn('Failed to clear favorites during account deletion:', e); + } + try { + await deleteProfile(); + } catch (e) { + console.warn('Failed to delete profile during account deletion:', e); + } + + // End sessions and reset local state + try { + await account.deleteSessions(); + } catch (e) { + console.warn('Failed to delete all sessions, continuing:', e); + } + try { + await account.deleteSession('current'); + } catch (e) { + console.warn('Failed to delete current session, continuing:', e); + } + + setUser(null); + setUserProfile(null); + mealLoggingService.setCurrentUser(null); + navigate('/'); + } catch (error) { + console.error('Delete account flow failed:', error); + throw error; + } + }; + const refreshUserSession = async () => { + try { + console.log('Refreshing user session...'); + const currentUser = await account.get(); + console.log('Session refreshed, user:', currentUser); + setUser(currentUser); + mealLoggingService.setCurrentUser(currentUser); + setError(null); + + // Try to get user profile + try { + await getUserProfile(currentUser.$id); + } catch (profileError) { + console.warn('Failed to load profile after session refresh:', profileError); + } + + return currentUser; + } catch (error) { + console.error('Failed to refresh user session:', error); + setUser(null); + setError(null); + throw error; + } + }; + const guestLogin = async () => { try { setError(null); @@ -324,9 +394,11 @@ export const AuthProvider = ({ children }) => { logout, signup, guestLogin, + refreshUserSession, updateProfile, getUserProfile, deleteProfile, + deleteAccount, loading, error, isAuthenticated: !!user, diff --git a/src/pages/Settings.jsx b/src/pages/Settings.jsx index 8b5012b..cae273c 100644 --- a/src/pages/Settings.jsx +++ b/src/pages/Settings.jsx @@ -2,7 +2,7 @@ import React, { useState } from 'react'; import { useAuth } from '../contexts/AuthContext'; import { Link } from 'react-router-dom'; const Settings = () => { - const { user, userProfile, updateProfile, logout } = useAuth(); + const { user, userProfile, updateProfile, logout, deleteAccount } = useAuth(); const [activeTab, setActiveTab] = useState('profile'); const [isLoading, setIsLoading] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); @@ -208,7 +208,6 @@ const Settings = () => { {activeTab === 'account' && (

Account Settings

-

Account Settings

Account Information

@@ -251,8 +250,16 @@ const Settings = () => { Cancel