Skip to content

Commit 38b19ad

Browse files
authored
fix: preserve user metadata when email_confirm=true in Confirm function
## Problem 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. ## Root Cause The current implementation calls `UpdateUserMetaData` with only `{"email_verified": true}`, which replaces the entire metadata object instead of merging with existing data. ## Solution 1. Reload the user's latest state from the database to get the most recent metadata 2. Merge the `email_verified: true` flag with existing metadata 3. Use the existing `UpdateUserMetaData` function to properly update without data loss ## Testing - ✅ Created user with `email_confirm: true` and custom metadata - ✅ Verified metadata is preserved and `email_verified: true` is merged - ✅ Confirmed fix works through Kong API gateway - ✅ Tested user creation, updates, and deletion operations ## Impact Fixes data loss issue where user metadata was being overwritten during email confirmation, ensuring all custom user data is preserved. Closes #2088
1 parent 079b242 commit 38b19ad

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

internal/models/user.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,24 @@ func (u *User) Confirm(tx *storage.Connection) error {
449449
return err
450450
}
451451

452-
if err := u.UpdateUserMetaData(tx, map[string]interface{}{
453-
"email_verified": true,
454-
}); err != nil {
452+
// 1. Reload the user state from the database to get the most recent
453+
// metadata, including any changes made by database triggers.
454+
latestUser, err := FindUserByID(tx, u.ID)
455+
if err != nil {
456+
return err
457+
}
458+
459+
// 2. Prepare the metadata for update. Start with the fresh data from the database.
460+
metaDataToUpdate := latestUser.UserMetaData
461+
if metaDataToUpdate == nil {
462+
metaDataToUpdate = make(map[string]interface{})
463+
}
464+
metaDataToUpdate["email_verified"] = true
465+
466+
// 3. Now, call the existing UpdateUserMetaData function.
467+
// This will correctly update the user's metadata without data loss,
468+
// and it also updates the in-memory user object 'u' for consistency.
469+
if err := u.UpdateUserMetaData(tx, metaDataToUpdate); err != nil {
455470
return err
456471
}
457472

0 commit comments

Comments
 (0)