From ff4a64d09bae16ca664b270ef48c2f87b37bfacc Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Sat, 5 Nov 2022 08:27:10 -0600 Subject: [PATCH] Updating how users are marked verified --- accounts/routes.go | 54 +++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/accounts/routes.go b/accounts/routes.go index fa07146..48e9b73 100644 --- a/accounts/routes.go +++ b/accounts/routes.go @@ -236,22 +236,8 @@ func (s *Service) LoginEmailConf(c echo.Context) error { return err } - if !user.IsVerified() { - // Verify email address here. - ctx := database.Context(c.Request().Context(), gctx.Server.DB) - if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { - // Null any current pending email changes - _, err := tx.ExecContext(ctx, ` - UPDATE "users" - SET is_verified=true - WHERE id=$1`, user.GetID()) - if err != nil { - return err - } - return nil - }); err != nil { - return err - } + if err := verifyUser(c, user); err != nil { + return err } if err := conf.Confirm(gctx.Server.DB); err != nil { @@ -452,6 +438,11 @@ func (s *Service) ForgotPasswordConf(c echo.Context) error { return err } + // Email confirmed, mark verified if needed + if err := verifyUser(c, user); err != nil { + return err + } + gmap := s.fetch.ForgotPasswordConfTemplateVars(c) gmap["guser"] = user gmap["key"] = key @@ -562,19 +553,7 @@ func (s *Service) ConfirmEmailConf(c echo.Context) error { return err } - ctx := database.Context(c.Request().Context(), gctx.Server.DB) - if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { - // Null any current pending email changes - _, err := tx.ExecContext(ctx, ` - UPDATE "users" - SET is_verified=true - WHERE id=$1`, - user.GetID()) - if err != nil { - return err - } - return nil - }); err != nil { + if err := verifyUser(c, user); err != nil { return err } @@ -589,6 +568,23 @@ func (s *Service) ConfirmEmailConf(c echo.Context) error { return c.Redirect(http.StatusMovedPermanently, s.fetch.RedirectAfterEmailConfirmaion(c)) } +func verifyUser(c echo.Context, user gobwebs.User) error { + if !user.IsVerified() { + gctx := c.(*server.Context) + ctx := database.Context(c.Request().Context(), gctx.Server.DB) + return database.WithTx(ctx, nil, func(tx *sql.Tx) error { + // Null any current pending email changes + _, err := tx.ExecContext(ctx, ` + UPDATE "users" + SET is_verified=true + WHERE id=$1`, + user.GetID()) + return err + }) + } + return nil +} + // UpdateEmail changes the password for an authenticated user func (s *Service) UpdateEmail(c echo.Context) error { gctx := c.(*server.Context) -- 2.45.2