Skip to content

Commit

Permalink
fix(jwt): add updated email on user create (#1416)
Browse files Browse the repository at this point in the history
* add check of email object is nil to DTO transformation - prevents runtime nil exception

* fetch updated emails for user before creating session token

Co-authored-by: Stefan Jacobi <[email protected]>
  • Loading branch information
shentschel and Stefan Jacobi authored Apr 3, 2024
1 parent 315c9aa commit 5ceb6bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/dto/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type EmailJwt struct {
}

func JwtFromEmailModel(email *models.Email) *EmailJwt {
if email == nil {
return nil
}

return &EmailJwt{
Address: email.Address,
IsPrimary: email.IsPrimary(),
Expand Down
8 changes: 7 additions & 1 deletion backend/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (h *UserHandler) Create(c echo.Context) error {
if !h.cfg.Emails.RequireVerification {
// Assign the email address to the user because it's currently unassigned and email verification is turned off.
email.UserID = &newUser.ID

err = h.persister.GetEmailPersisterWithConnection(tx).Update(*email)
if err != nil {
return fmt.Errorf("failed to update email address: %w", err)
Expand Down Expand Up @@ -104,8 +105,13 @@ func (h *UserHandler) Create(c echo.Context) error {
return fmt.Errorf("failed to store primary email: %w", err)
}

emails, err := h.persister.GetEmailPersisterWithConnection(tx).FindByUserId(newUser.ID)
if err != nil {
return fmt.Errorf("failed to get email from db: %w", err)
}

var emailJwt *dto.EmailJwt
if e := newUser.Emails.GetPrimary(); e != nil {
if e := emails.GetPrimary(); e != nil {
emailJwt = dto.JwtFromEmailModel(e)
}

Expand Down

0 comments on commit 5ceb6bb

Please sign in to comment.