Issue: Time entries created while logged in were being deleted from the Supabase database when users logged out and logged back in.
The problem was in the data synchronization logic between localStorage and Supabase during authentication state changes:
- User logs in: Data service switches to
SupabaseService - User creates entries: Saved to Supabase ✅
- User logs out:
- Data service switches to
LocalStorageService - NO data sync from Supabase to localStorage
- User works with empty localStorage
- Data service switches to
- User logs back in:
- Data service switches back to
SupabaseService migrateFromLocalStorage()runs and could overwrite Supabase data!⚠️
- Data service switches back to
- Smart Data Detection: Only migrates if localStorage has meaningful data
- Conflict Prevention: Checks existing Supabase data before overwriting
- Cautious Merging: Only replaces Supabase data if localStorage has MORE data
- Detailed Logging: Comprehensive logging for debugging
// Before: Blindly overwrote Supabase with localStorage
if (currentDay) {
await this.saveCurrentDay(currentDay);
}
// After: Smart conflict resolution
const shouldMigrateCurrentDay = hasCurrentDay && (!existingCurrentDay ||
currentDay!.tasks.length > existingCurrentDay.tasks.length);
if (shouldMigrateCurrentDay) {
await this.saveCurrentDay(currentDay!);
}- New Method: Added
migrateToLocalStorage()to sync Supabase data TO localStorage - Offline Continuity: Users can continue working offline after logout
- Data Safety: Ensures no data loss during authentication transitions
- State Tracking: Track previous authentication state
- Logout Detection: Detect when user transitions from authenticated to not authenticated
- Automatic Sync: Automatically sync data to localStorage before switching services
// Detect logout: was authenticated, now not authenticated
if (previousAuthState === true && !isAuthenticated && dataService) {
await dataService.migrateToLocalStorage();
}- ✅ Prevents accidental data loss during logout/login cycles
- ✅ No more overwriting Supabase data with empty localStorage
- ✅ Smart conflict resolution protects existing data
- ✅ Seamless offline/online transitions
- ✅ Data persists across authentication state changes
- ✅ Users can continue working after logout
- ✅ Comprehensive logging for debugging
- ✅ Clear separation of concerns
- ✅ Robust error handling
To verify the fix works:
-
Login and Create Data:
- Log in to the app
- Create some time entries
- Verify they appear in Supabase
-
Logout Test:
- Log out
- Check browser console for sync messages
- Verify data is now in localStorage
-
Login Again:
- Log back in
- Verify original Supabase data is preserved
- Check that no data was lost
-
Offline Work Test:
- Log out
- Create new entries (stored in localStorage)
- Log back in
- Verify new entries are merged without losing old ones
-
/src/services/dataService.ts:- Enhanced
migrateFromLocalStorage()with smart conflict resolution - Added
migrateToLocalStorage()method - Added comprehensive logging and safety checks
- Enhanced
-
/src/contexts/TimeTrackingContext.tsx:- Added
previousAuthStatetracking - Enhanced auth state change detection
- Automatic data sync on logout
- Added
The fix adds detailed console logging to help debug and monitor the data synchronization:
🔄 Checking for localStorage data to migrate...⚠️ Supabase already contains data - being cautious with migration✅ No meaningful localStorage data found - skipping migration🔄 User logged out - syncing data to localStorage for offline access
This fix ensures that user data is preserved across all authentication state changes, providing a robust and user-friendly experience while maintaining data integrity in both localStorage and Supabase.