~netlandish/gobwebs

ff4a64d09bae16ca664b270ef48c2f87b37bfacc — Peter Sanchez 2 years ago 7babede
Updating how users are marked verified
1 files changed, 25 insertions(+), 29 deletions(-)

M accounts/routes.go
M accounts/routes.go => accounts/routes.go +25 -29
@@ 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)