From 5ceb6bb03067f0214b3c92ca4d78042beb9d6b0e Mon Sep 17 00:00:00 2001 From: Stefan Jacobi Date: Wed, 3 Apr 2024 16:15:25 +0200 Subject: [PATCH] fix(jwt): add updated email on user create (#1416) * 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 --- backend/dto/email.go | 4 ++++ backend/handler/user.go | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/dto/email.go b/backend/dto/email.go index 5ab63e9dc..29ef4639a 100644 --- a/backend/dto/email.go +++ b/backend/dto/email.go @@ -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(), diff --git a/backend/handler/user.go b/backend/handler/user.go index f9838f82a..1cdaddcbc 100644 --- a/backend/handler/user.go +++ b/backend/handler/user.go @@ -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) @@ -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) }