~netlandish/gobwebs

0ae025cadb32e7642dd77f977c1f83e8e3cb93f1 — Peter Sanchez 2 years ago fd293b1
Adding functions to populate template contexts for account handlers.
2 files changed, 69 insertions(+), 65 deletions(-)

M accounts/routes.go
M interfaces.go
M accounts/routes.go => accounts/routes.go +62 -65
@@ 22,7 22,7 @@ type Service struct {
	eg    *echo.Group
}

//RegisterRoutes ...
// RegisterRoutes ...
func (s *Service) RegisterRoutes() {
	s.eg.GET("/login", s.Login).Name = s.RouteName("login")
	s.eg.POST("/login", s.LoginPOST).Name = s.RouteName("login_post")


@@ 80,9 80,9 @@ func (s *Service) Login(c echo.Context) error {
func (s *Service) LoginAuth(c echo.Context) error {
	gctx := c.(*server.Context)
	next := c.QueryParam("next")
	return gctx.Render(http.StatusOK, "login.html", gobwebs.Map{
		"next": next,
	})
	gmap := s.fetch.LoginAuthTemplateVars(c)
	gmap["next"] = next
	return gctx.Render(http.StatusOK, "login.html", gmap)
}

// LoginEmail ...


@@ 93,9 93,9 @@ func (s *Service) LoginEmail(c echo.Context) error {
	}

	sent := c.QueryParam("sent")
	return gctx.Render(http.StatusOK, "login_email.html", gobwebs.Map{
		"sent": sent,
	})
	gmap := s.fetch.LoginEmailTemplateVars(c)
	gmap["sent"] = sent
	return gctx.Render(http.StatusOK, "login_email.html", gmap)
}

// LoginPOST ...


@@ 115,13 115,13 @@ func (s *Service) LoginPOST(c echo.Context) error {
func (s *Service) LoginAuthPOST(c echo.Context) error {
	gctx := c.(*server.Context)
	form := &LoginForm{}
	gmap := s.fetch.LoginAuthTemplateVars(c)
	if err := form.Validate(c, s.fetch); err != nil {
		switch err.(type) {
		case validate.InputErrors:
			return gctx.Render(http.StatusOK, "login.html", gobwebs.Map{
				"errors": err,
				"form":   form,
			})
			gmap["errors"] = err
			gmap["form"] = form
			return gctx.Render(http.StatusOK, "login.html", gmap)
		default:
			// Raise ISE if error is unrelated to input validation
			return err


@@ 149,13 149,13 @@ func (s *Service) LoginEmailPOST(c echo.Context) error {
	}

	form := &LoginEmailForm{}
	gmap := s.fetch.LoginAuthTemplateVars(c)
	if err := form.Validate(c); err != nil {
		switch err.(type) {
		case validate.InputErrors:
			return gctx.Render(http.StatusOK, "login_email.html", gobwebs.Map{
				"errors": err,
				"form":   form,
			})
			gmap["errors"] = err
			gmap["form"] = form
			return gctx.Render(http.StatusOK, "login_email.html", gmap)
		default:
			// Raise ISE if error is unrelated to input validation
			return err


@@ 186,17 186,15 @@ func (s *Service) LoginEmailPOST(c echo.Context) error {
				"email_login_email_html.html",
			},
		}
		data := gobwebs.Map{
			"guser":        user,
			"confirmation": conf,
		}
		gmap["guser"] = user
		gmap["confirmation"] = conf
		helper := email.NewHelper(gctx.Server.Email, tmap, tmpl)
		helper.IgnoreHTMLErrors = true
		err = helper.Send(
			"email_login_email",
			gctx.Server.Config.DefaultFromEmail,
			form.Email,
			data,
			gmap,
		)
		if err != nil {
			return err


@@ 272,20 270,21 @@ func (s *Service) LoginEmailConf(c echo.Context) error {
// ChangePassword changes the password for an authenticated user
func (s *Service) ChangePassword(c echo.Context) error {
	gctx := c.(*server.Context)
	return gctx.Render(http.StatusOK, "change_password.html", gobwebs.Map{})
	gmap := s.fetch.ChangePasswordTemplateVars(c)
	return gctx.Render(http.StatusOK, "change_password.html", gmap)
}

// ChangePasswordPOST changes the password for an authenticated user
func (s *Service) ChangePasswordPOST(c echo.Context) error {
	gctx := c.(*server.Context)
	form := &ChangePasswordForm{}
	gmap := s.fetch.ChangePasswordTemplateVars(c)
	if err := form.Validate(c); err != nil {
		switch err.(type) {
		case validate.InputErrors:
			return gctx.Render(http.StatusOK, "change_password.html", gobwebs.Map{
				"errors": err,
				"form":   form,
			})
			gmap["errors"] = err
			gmap["form"] = form
			return gctx.Render(http.StatusOK, "change_password.html", gmap)
		default:
			// Raise ISE if error is unrelated to input validation
			return err


@@ 307,9 306,8 @@ func (s *Service) ChangePasswordPOST(c echo.Context) error {
		return c.Redirect(http.StatusMovedPermanently, next)
	}

	return gctx.Render(http.StatusOK, "change_password.html", gobwebs.Map{
		"form": form,
	})
	gmap["form"] = form
	return gctx.Render(http.StatusOK, "change_password.html", gmap)
}

// ForgotPassword ...


@@ 325,9 323,9 @@ func (s *Service) ForgotPassword(c echo.Context) error {
	}

	sent := c.QueryParam("sent")
	return gctx.Render(http.StatusOK, "forgot_password.html", gobwebs.Map{
		"sent": sent,
	})
	gmap := s.fetch.ForgotPasswordTemplateVars(c)
	gmap["sent"] = sent
	return gctx.Render(http.StatusOK, "forgot_password.html", gmap)
}

// ForgotPasswordPOST ...


@@ 338,13 336,13 @@ func (s *Service) ForgotPasswordPOST(c echo.Context) error {
	}

	form := &LoginEmailForm{}
	gmap := s.fetch.ForgotPasswordTemplateVars(c)
	if err := form.Validate(c); err != nil {
		switch err.(type) {
		case validate.InputErrors:
			return gctx.Render(http.StatusOK, "forgot_password.html", gobwebs.Map{
				"errors": err,
				"form":   form,
			})
			gmap["errors"] = err
			gmap["form"] = form
			return gctx.Render(http.StatusOK, "forgot_password.html", gmap)
		default:
			// Raise ISE if error is unrelated to input validation
			return err


@@ 404,17 402,15 @@ func (s *Service) ForgotPasswordPOST(c echo.Context) error {
				"email_forgot_password_html.html",
			},
		}
		data := gobwebs.Map{
			"guser":        user,
			"confirmation": conf,
		}
		gmap["guser"] = user
		gmap["confirmation"] = conf
		helper := email.NewHelper(gctx.Server.Email, tmap, tmpl)
		helper.IgnoreHTMLErrors = true
		err = helper.Send(
			"email_forgot_password",
			gctx.Server.Config.DefaultFromEmail,
			form.Email,
			data,
			gmap,
		)
		if err != nil {
			return err


@@ 456,10 452,10 @@ func (s *Service) ForgotPasswordConf(c echo.Context) error {
		return err
	}

	return gctx.Render(http.StatusOK, "forgot_password_conf.html", gobwebs.Map{
		"guser": user,
		"key":   key,
	})
	gmap := s.fetch.ForgotPasswordConfTemplateVars(c)
	gmap["guser"] = user
	gmap["key"] = key
	return gctx.Render(http.StatusOK, "forgot_password_conf.html", gmap)
}

// ForgotPasswordConfPOST changes the password for an authenticated user


@@ 493,9 489,14 @@ func (s *Service) ForgotPasswordConfPOST(c echo.Context) error {
	}

	form := &ResetPasswordForm{}
	gmap := s.fetch.ForgotPasswordConfTemplateVars(c)
	if err := form.Validate(c); err != nil {
		switch err.(type) {
		case validate.InputErrors:
			gmap["errors"] = err
			gmap["form"] = form
			gmap["guser"] = user
			gmap["key"] = key
			return gctx.Render(http.StatusOK, "forgot_password_conf.html", gobwebs.Map{
				"errors": err,
				"form":   form,


@@ 526,7 527,7 @@ func (s *Service) ForgotPasswordConfPOST(c echo.Context) error {
		return c.Redirect(http.StatusMovedPermanently, next)
	}

	return gctx.Render(http.StatusOK, "forgot_password_conf_done.html", gobwebs.Map{})
	return gctx.Render(http.StatusOK, "forgot_password_conf_done.html", gmap)
}

// ConfirmEmailConf confirms a users email and marks them as verified


@@ 594,9 595,9 @@ func (s *Service) UpdateEmail(c echo.Context) error {
	}

	sent := c.QueryParam("sent")
	return gctx.Render(http.StatusOK, "update_email.html", gobwebs.Map{
		"sent": sent,
	})
	gmap := s.fetch.UpdateEmailTemplateVars(c)
	gmap["sent"] = sent
	return gctx.Render(http.StatusOK, "update_email.html", gmap)
}

// UpdateEmailPOST changes the password for an authenticated user


@@ 607,13 608,13 @@ func (s *Service) UpdateEmailPOST(c echo.Context) error {
	}

	form := &LoginEmailForm{}
	gmap := s.fetch.UpdateEmailTemplateVars(c)
	if err := form.Validate(c); err != nil {
		switch err.(type) {
		case validate.InputErrors:
			return gctx.Render(http.StatusOK, "update_email.html", gobwebs.Map{
				"errors": err,
				"form":   form,
			})
			gmap["errors"] = err
			gmap["form"] = form
			return gctx.Render(http.StatusOK, "update_email.html", gmap)
		default:
			// Raise ISE if error is unrelated to input validation
			return err


@@ 628,10 629,9 @@ func (s *Service) UpdateEmailPOST(c echo.Context) error {
	} else {
		// Email already exists in the system
		nerr := fmt.Errorf("This email is currently registered to an existing account")
		return gctx.Render(http.StatusOK, "update_email.html", gobwebs.Map{
			"errors": validate.GetInputErrors([]error{nerr}),
			"form":   form,
		})
		gmap["errors"] = validate.GetInputErrors([]error{nerr})
		gmap["form"] = form
		return gctx.Render(http.StatusOK, "update_email.html", gmap)
	}

	// Email is not in our system. Verify there is no pending


@@ 679,10 679,9 @@ func (s *Service) UpdateEmailPOST(c echo.Context) error {
	if pid != 0 {
		// Email already exists in pending email change
		nerr := fmt.Errorf("This email is currently pending confirmation for another account")
		return gctx.Render(http.StatusOK, "update_email.html", gobwebs.Map{
			"errors": validate.GetInputErrors([]error{nerr}),
			"form":   form,
		})
		gmap["errors"] = validate.GetInputErrors([]error{nerr})
		gmap["form"] = form
		return gctx.Render(http.StatusOK, "update_email.html", gmap)
	}

	// All looks good. Send confirmation email


@@ 704,17 703,15 @@ func (s *Service) UpdateEmailPOST(c echo.Context) error {
			"email_update_email_html.html",
		},
	}
	data := gobwebs.Map{
		"guser":        gctx.User,
		"confirmation": conf,
	}
	gmap["guser"] = gctx.User
	gmap["confirmation"] = conf
	helper := email.NewHelper(gctx.Server.Email, tmap, tmpl)
	helper.IgnoreHTMLErrors = true
	err = helper.Send(
		"email_update_email",
		gctx.Server.Config.DefaultFromEmail,
		gctx.User.GetEmail(),
		data,
		gmap,
	)
	if err != nil {
		return err

M interfaces.go => interfaces.go +7 -0
@@ 59,4 59,11 @@ type UserFetch interface {
	RedirectAfterPasswordReset(c echo.Context) string
	RedirectAfterEmailUpdate(c echo.Context) string
	RedirectAfterEmailConfirmaion(c echo.Context) string

	LoginAuthTemplateVars(c echo.Context) Map
	LoginEmailTemplateVars(c echo.Context) Map
	ForgotPasswordTemplateVars(c echo.Context) Map
	ForgotPasswordConfTemplateVars(c echo.Context) Map
	ChangePasswordTemplateVars(c echo.Context) Map
	UpdateEmailTemplateVars(c echo.Context) Map
}