auth icon indicating copy to clipboard operation
auth copied to clipboard

fix: preserve user metadata when email_confirm=true in Confirm function

Open rishiraj-58 opened this issue 5 months ago • 1 comments

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

When creating a user with email_confirm: true, the Confirm function in internal/models/user.go overwrites the user's existing metadata instead of merging the email_verified: true flag with it.

#2088

Root cause

The current implementation calls UpdateUserMetaData with only {"email_verified": true}, which replaces the entire metadata object instead of merging with existing data.

Example of data loss

// Before email confirmation
{
  "user_metadata": {
    "name": "John Doe",
    "age": 25,
    "preferences": {
      "theme": "dark",
      "language": "en"
    }
  }
}

// After email confirmation (BROKEN - data lost!)
{
  "user_metadata": {
    "email_verified": true
  }
}

What is the new behavior?

The Confirm function now properly preserves existing user metadata by:

  1. Reloading the user's latest state from the database to get the most recent metadata
  2. Merging the email_verified: true flag with existing metadata
  3. Using the existing UpdateUserMetaData function to properly update without data loss

Example of correct behaviour:

// Before email confirmation
{
  "user_metadata": {
    "name": "John Doe",
    "age": 25,
    "preferences": {
      "theme": "dark",
      "language": "en"
    }
  }
}

// After email confirmation (FIXED - data preserved!)
{
  "user_metadata": {
    "name": "John Doe",
    "age": 25,
    "email_verified": true,
    "preferences": {
      "theme": "dark",
      "language": "en"
    }
  }
}

rishiraj-58 avatar Jul 24 '25 17:07 rishiraj-58