8000 Critical: Implement spending data migration for anonymous to authenticated users · Issue #48 · laminko/SpendCheck · GitHub
[go: up one dir, main page]

Skip to content

Critical: Implement spending data migration for anonymous to authenticated users #48

Closed

Description

@laminko

Problem

Currently, when anonymous users sign in to create an account, they lose all their existing spending data. This is a critical user experience issue that could frustrate users and cause data loss.

Current Behavior

  1. User tracks spending without signing in (data stored with anonymous user_id)
  2. User signs in to access data on other devices
  3. User gets new authenticated user_id
  4. All previous spending data is lost (remains tied to old anonymous user_id)
  5. User starts with empty spending history

What Currently Works

  • ✅ Currency preference migration (localStorage → database)
  • ✅ User can access account on other devices
  • ✅ Robust authentication system

What's Missing

  • ❌ Spending data migration from anonymous to authenticated user_id
  • ❌ Warning users about potential data loss before authentication

Technical Details

Root Cause:

  • Spending entries are stored with user_id in spending_entries table
  • Anonymous users get one user_id, authenticated users get a different user_id
  • No migration logic exists to transfer spending data between user IDs

Files Involved:

  • src/composables/useAuth.ts - Authentication state management
  • src/composables/useSpendingStore.ts - Spending data storage
  • src/composables/useUserPreferences.ts - Has working migration example

Reference Implementation:
The useUserPreferences.ts already has a working migrateGuestPreferences() function that migrates currency settings. This can serve as a template for spending data migration.

Proposed Solution

Implement a migrateSpendingData() function that:

  1. Capture anonymous user_id before authentication transition
  2. Transfer spending entries from anonymous user_id to authenticated user_id
  3. Verify data integrity during migration
  4. Handle migration failures gracefully
  5. Clean up orphaned data after successful migration

Implementation Approach

// In useSpendingStore.ts
const migrateAnonymousSpendingData = async (oldUserId: string, newUserId: string) => {
  try {
    // Update all spending_entries from old user_id to new user_id
    const { error } = await supabase
      .from('spending_entries')
      .update({ user_id: newUserId })
      .eq('user_id', oldUserId)
    
    if (error) throw error
    
    // Reload entries for new user
    await loadEntries()
  } catch (error) {
    console.error('Failed to migrate spending data:', error)
    // Handle migration failure
  }
}

Acceptance Criteria

  • Anonymous users can sign in without losing spending data
  • All spending entries are transferred to authenticated account
  • Migration handles errors gracefully
  • Data integrity is maintained during migration
  • User sees seamless transition (no data loss)
  • Migration works across all authentication methods (email, Google, Facebook)

Priority

High Priority - This is a critical user experience issue that could cause significant user frustration and data loss.

Related

  • Part of the authentication system completion
  • Mentioned in CLAUDE.md as remaining implementation work: "Data Migration - Moving guest data to authenticated accounts"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0