From ecb232cd3fb2574f9430ce2c4cb8f1fb2e56e532 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:41:33 -0500 Subject: [PATCH 1/3] remove duplicate UserResponse struct --- backend/internal/server/auth.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/backend/internal/server/auth.go b/backend/internal/server/auth.go index e25cf867..0380bbfc 100644 --- a/backend/internal/server/auth.go +++ b/backend/internal/server/auth.go @@ -18,23 +18,13 @@ import ( ) type SignupResponse struct { - AccessToken string `json:"access_token"` - User UserResponse `json:"user"` + AccessToken string `json:"access_token"` + User User `json:"user"` } type SigninResponse struct { - AccessToken string `json:"access_token"` - User UserResponse `json:"user"` -} - -type UserResponse struct { - ID string `json:"id"` - Email string `json:"email"` - FirstName *string `json:"first_name"` - LastName *string `json:"last_name"` - Role db.UserRole `json:"role"` - WalletAddress *string `json:"wallet_address,omitempty"` - EmailVerified bool `json:"email_verified"` + AccessToken string `json:"access_token"` + User User `json:"user"` } func (s *Server) setupAuthRoutes() { @@ -127,7 +117,7 @@ func (s *Server) handleSignup(c echo.Context) error { return c.JSON(http.StatusCreated, SignupResponse{ AccessToken: accessToken, - User: UserResponse{ + User: User{ ID: user.ID, Email: user.Email, FirstName: user.FirstName, @@ -180,7 +170,7 @@ func (s *Server) handleSignin(c echo.Context) error { return c.JSON(http.StatusOK, SigninResponse{ AccessToken: accessToken, - User: UserResponse{ + User: User{ ID: user.ID, Email: user.Email, FirstName: user.FirstName, From b3cf1eaae0f14cfd205f60bc566355b5b7acc413 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:47:01 -0500 Subject: [PATCH 2/3] remove redundant GetUserTokenSalt call --- backend/internal/server/auth.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/backend/internal/server/auth.go b/backend/internal/server/auth.go index 0380bbfc..8700d41e 100644 --- a/backend/internal/server/auth.go +++ b/backend/internal/server/auth.go @@ -65,13 +65,7 @@ func (s *Server) handleSignup(c echo.Context) error { return echo.NewHTTPError(http.StatusConflict, "email already exists") } - // Get user's token salt that was generated during creation - salt, err := s.queries.GetUserTokenSalt(ctx, user.ID) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user's token salt") - } - - accessToken, refreshToken, err := jwt.GenerateWithSalt(user.ID, user.Role, salt) + accessToken, refreshToken, err := jwt.GenerateWithSalt(user.ID, user.Role, user.TokenSalt) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "failed to generate tokens") } @@ -145,13 +139,7 @@ func (s *Server) handleSignin(c echo.Context) error { return echo.NewHTTPError(http.StatusUnauthorized, "invalid credentials") } - // Get user's token salt - salt, err := s.queries.GetUserTokenSalt(ctx, user.ID) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user's token salt") - } - - accessToken, refreshToken, err := jwt.GenerateWithSalt(user.ID, user.Role, salt) + accessToken, refreshToken, err := jwt.GenerateWithSalt(user.ID, user.Role, user.TokenSalt) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "failed to generate tokens") } From 22f21ba59ae42f9fac82caf0081366e54cc3a341 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:49:55 -0500 Subject: [PATCH 3/3] timeout context for async email verification --- backend/internal/server/auth.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/internal/server/auth.go b/backend/internal/server/auth.go index 8700d41e..979001a9 100644 --- a/backend/internal/server/auth.go +++ b/backend/internal/server/auth.go @@ -84,7 +84,10 @@ func (s *Server) handleSignup(c echo.Context) error { // Send verification email asynchronously go func() { - token, err := s.queries.CreateVerifyEmailToken(context.Background(), db.CreateVerifyEmailTokenParams{ + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + token, err := s.queries.CreateVerifyEmailToken(ctx, db.CreateVerifyEmailTokenParams{ Email: user.Email, ExpiresAt: time.Now().Add(30 * time.Minute), }) @@ -101,7 +104,7 @@ func (s *Server) handleSignup(c echo.Context) error { } // Send verification email - if err := service.SendVerficationEmail(context.Background(), user.Email, tokenStr); err != nil { + if err := service.SendVerficationEmail(ctx, user.Email, tokenStr); err != nil { log.Error().Err(err).Msg("Failed to send verification email") return }