Skip to content

Commit

Permalink
fix: update phone if autoconfirm is enabled (#1431)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* In #1407, the update user endpoint was refactored to use `smsVerify`
if `GOTRUE_SMS_AUTOCONFIRM` is enabled. However, `smsVerify` doesn't
update the user's phone number to the new one. This PR aims to fix that.
  • Loading branch information
kangmingtay committed Feb 15, 2024
1 parent 4d3b9b8 commit 95db770
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {

if params.Phone != "" && params.Phone != user.GetPhone() {
if config.Sms.Autoconfirm {
user.PhoneChange = params.Phone
if _, terr := a.smsVerify(r, ctx, tx, user, &VerifyParams{
Type: phoneChangeVerification,
Phone: params.Phone,
Expand Down
7 changes: 7 additions & 0 deletions internal/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ func (ts *UserTestSuite) TestUserUpdatePhoneAutoconfirmEnabled() {
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
require.Equal(ts.T(), c.expectedCode, w.Code)

if c.expectedCode == http.StatusOK {
// check that the user response returned contains the updated phone field
data := &models.User{}
require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data))
require.Equal(ts.T(), data.GetPhone(), c.userData["phone"])
}
})
}

Expand Down
6 changes: 0 additions & 6 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,6 @@ func (u *User) UpdatePassword(tx *storage.Connection, sessionID *uuid.UUID) erro
}
}

// UpdatePhone updates the user's phone
func (u *User) UpdatePhone(tx *storage.Connection, phone string) error {
u.Phone = storage.NullString(phone)
return tx.UpdateOnly(u, "phone")
}

// Authenticate a user from a password
func (u *User) Authenticate(ctx context.Context, password string) bool {
err := crypto.CompareHashAndPassword(ctx, u.EncryptedPassword, password)
Expand Down

0 comments on commit 95db770

Please sign in to comment.