Skip to content

Commit 483463e

Browse files
authored
fix: email_verified field not being updated on signup confirmation (#1868)
## What kind of change does this PR introduce? * Addresses #1620
1 parent 40e0de1 commit 483463e

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

internal/api/verify.go

+19
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,25 @@ func (a *API) signupVerify(r *http.Request, ctx context.Context, conn *storage.C
328328
if terr = user.Confirm(tx); terr != nil {
329329
return internalServerError("Error confirming user").WithInternalError(terr)
330330
}
331+
332+
// on signupVerify, the user will always only have an email identity
333+
// so we can safely assume that the first identity is the email identity
334+
//
335+
// we still check for the length of the identities slice to be safe.
336+
if len(user.Identities) != 0 {
337+
if len(user.Identities) > 1 {
338+
return internalServerError("User has more than one identity on signup")
339+
}
340+
emailIdentity := user.Identities[0]
341+
if emailIdentity.Email != user.Email {
342+
return internalServerError("User email identity does not match user email")
343+
}
344+
if terr = emailIdentity.UpdateIdentityData(tx, map[string]interface{}{
345+
"email_verified": true,
346+
}); terr != nil {
347+
return internalServerError("Error updating email identity").WithInternalError(terr)
348+
}
349+
}
331350
return nil
332351
})
333352
if err != nil {

internal/api/verify_test.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ func (ts *VerifyTestSuite) SetupTest() {
4848
u, err := models.NewUser("12345678", "[email protected]", "password", ts.Config.JWT.Aud, nil)
4949
require.NoError(ts.T(), err, "Error creating test user model")
5050
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new test user")
51+
52+
// Create identity
53+
i, err := models.NewIdentity(u, "email", map[string]interface{}{
54+
"sub": u.ID.String(),
55+
"email": "[email protected]",
56+
"email_verified": false,
57+
})
58+
require.NoError(ts.T(), err, "Error creating test identity model")
59+
require.NoError(ts.T(), ts.API.db.Create(i), "Error saving new test identity")
5160
}
5261

5362
func (ts *VerifyTestSuite) TestVerifyPasswordRecovery() {
@@ -673,6 +682,8 @@ func (ts *VerifyTestSuite) TestVerifySignupWithRedirectURLContainedPath() {
673682
u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
674683
require.NoError(ts.T(), err)
675684
assert.True(ts.T(), u.IsConfirmed())
685+
assert.True(ts.T(), u.UserMetaData["email_verified"].(bool))
686+
assert.True(ts.T(), u.Identities[0].IdentityData["email_verified"].(bool))
676687
})
677688
}
678689
}
@@ -875,6 +886,18 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
875886
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
876887
},
877888
},
889+
{
890+
desc: "Valid Signup Token Hash",
891+
sentTime: time.Now(),
892+
body: map[string]interface{}{
893+
"type": mail.SignupVerification,
894+
"token_hash": crypto.GenerateTokenHash(u.GetEmail(), "123456"),
895+
},
896+
expected: expected{
897+
code: http.StatusOK,
898+
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
899+
},
900+
},
878901
{
879902
desc: "Valid Recovery OTP",
880903
sentTime: time.Now(),
@@ -940,18 +963,6 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
940963
tokenHash: crypto.GenerateTokenHash(u.PhoneChange, "123456"),
941964
},
942965
},
943-
{
944-
desc: "Valid Signup Token Hash",
945-
sentTime: time.Now(),
946-
body: map[string]interface{}{
947-
"type": mail.SignupVerification,
948-
"token_hash": crypto.GenerateTokenHash(u.GetEmail(), "123456"),
949-
},
950-
expected: expected{
951-
code: http.StatusOK,
952-
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
953-
},
954-
},
955966
{
956967
desc: "Valid Email Change Token Hash",
957968
sentTime: time.Now(),

internal/models/user.go

+6
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ 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 {
455+
return err
456+
}
457+
452458
if err := ClearAllOneTimeTokensForUser(tx, u.ID); err != nil {
453459
return err
454460
}

0 commit comments

Comments
 (0)