diff --git a/internal/api/identity.go b/internal/api/identity.go index 42aca020f..0fe0560a7 100644 --- a/internal/api/identity.go +++ b/internal/api/identity.go @@ -134,9 +134,12 @@ func (a *API) linkIdentityToUser(r *http.Request, ctx context.Context, tx *stora if terr := targetUser.Confirm(tx); terr != nil { return nil, terr } - // if the user is an anonymous user, we need to reload the user object again to fetch the is_anonymous value - if terr := tx.Reload(targetUser); terr != nil { - return nil, terr + + if targetUser.IsAnonymous { + targetUser.IsAnonymous = false + if terr := tx.UpdateOnly(targetUser, "is_anonymous"); terr != nil { + return nil, terr + } } } diff --git a/internal/api/signup.go b/internal/api/signup.go index 4205a3016..bd12b80b2 100644 --- a/internal/api/signup.go +++ b/internal/api/signup.go @@ -82,6 +82,7 @@ func (params *SignupParams) ToUserModel(isSSOUser bool) (user *models.User, err user, err = models.NewUser(params.Phone, "", params.Password, params.Aud, params.Data) case "anonymous": user, err = models.NewUser("", "", "", params.Aud, params.Data) + user.IsAnonymous = true default: // handles external provider case user, err = models.NewUser("", params.Email, params.Password, params.Aud, params.Data) diff --git a/internal/api/verify.go b/internal/api/verify.go index 682fb3ea4..3e99dc9a4 100644 --- a/internal/api/verify.go +++ b/internal/api/verify.go @@ -531,6 +531,12 @@ func (a *API) emailChangeVerify(r *http.Request, ctx context.Context, conn *stor return terr } } + if user.IsAnonymous { + user.IsAnonymous = false + if terr := tx.UpdateOnly(user, "is_anonymous"); terr != nil { + return terr + } + } if terr := tx.Load(user, "Identities"); terr != nil { return internalServerError("Error refetching identities").WithInternalError(terr) } diff --git a/internal/models/user.go b/internal/models/user.go index 4fe84698f..5a6d0da66 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -66,7 +66,7 @@ type User struct { UpdatedAt time.Time `json:"updated_at" db:"updated_at"` BannedUntil *time.Time `json:"banned_until,omitempty" db:"banned_until"` DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` - IsAnonymous bool `json:"is_anonymous" db:"is_anonymous" rw:"r"` + IsAnonymous bool `json:"is_anonymous" db:"is_anonymous"` DONTUSEINSTANCEID uuid.UUID `json:"-" db:"instance_id"` } diff --git a/migrations/20240214120130_add_is_anonymous_column.up.sql b/migrations/20240214120130_add_is_anonymous_column.up.sql index ad7852e23..6ef963f57 100644 --- a/migrations/20240214120130_add_is_anonymous_column.up.sql +++ b/migrations/20240214120130_add_is_anonymous_column.up.sql @@ -1,12 +1,7 @@ do $$ begin alter table {{ index .Options "Namespace" }}.users - add column if not exists is_anonymous boolean generated always as ( - case - when (email is null or email = '') and (phone is null or phone = '') then true - else false - end - ) stored; + add column if not exists is_anonymous boolean not null default false; create index if not exists users_is_anonymous_idx on {{ index .Options "Namespace" }}.users using btree (is_anonymous); end