Refactored all forms to follow new pattern.

This commit is contained in:
mikestefanello 2021-12-24 08:42:42 -05:00
parent d5adf010db
commit 6f50552a15
11 changed files with 133 additions and 195 deletions

View file

@ -46,8 +46,6 @@ func (c *Contact) Post(ctx echo.Context) error {
return c.Fail(ctx, err, "unable to process form submission")
}
//ctx.Set(context.FormKey, form)
if !form.Submission.HasErrors() {
if err := c.Container.Mail.Send(ctx, form.Email, "Hello!"); err != nil {
return c.Fail(ctx, err, "unable to send email")

View file

@ -18,76 +18,79 @@ type (
}
ForgotPasswordForm struct {
Email string `form:"email" validate:"required,email" label:"Email address"`
Email string `form:"email" validate:"required,email"`
Submission controller.FormSubmission
}
)
func (f *ForgotPassword) Get(c echo.Context) error {
p := controller.NewPage(c)
p.Layout = "auth"
p.Name = "forgot-password"
p.Title = "Forgot password"
p.Data = ForgotPasswordForm{}
func (c *ForgotPassword) Get(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = "auth"
page.Name = "forgot-password"
page.Title = "Forgot password"
page.Form = ForgotPasswordForm{}
if form := c.Get(context.FormKey); form != nil {
p.Data = form.(ForgotPasswordForm)
if form := ctx.Get(context.FormKey); form != nil {
page.Form = form.(*ForgotPasswordForm)
}
return f.RenderPage(c, p)
return c.RenderPage(ctx, page)
}
func (f *ForgotPassword) Post(c echo.Context) error {
fail := func(message string, err error) error {
c.Logger().Errorf("%s: %v", message, err)
msg.Danger(c, "An error occurred. Please try again.")
return f.Get(c)
}
func (c *ForgotPassword) Post(ctx echo.Context) error {
var form ForgotPasswordForm
ctx.Set(context.FormKey, &form)
succeed := func() error {
c.Set(context.FormKey, nil)
msg.Success(c, "An email containing a link to reset your password will be sent to this address if it exists in our system.")
return f.Get(c)
ctx.Set(context.FormKey, nil)
msg.Success(ctx, "An email containing a link to reset your password will be sent to this address if it exists in our system.")
return c.Get(ctx)
}
// Parse the form values
var form ForgotPasswordForm
if err := c.Bind(&form); err != nil {
return fail("unable to parse forgot password form", err)
if err := ctx.Bind(&form); err != nil {
return c.Fail(ctx, err, "unable to parse forgot password form")
}
c.Set(context.FormKey, form)
// Validate the form
if err := c.Validate(form); err != nil {
f.SetValidationErrorMessages(c, err, form)
return f.Get(c)
if err := form.Submission.Process(ctx, form); err != nil {
return c.Fail(ctx, err, "unable to process form submission")
}
if form.Submission.HasErrors() {
return c.Get(ctx)
}
// Attempt to load the user
u, err := f.Container.ORM.User.
u, err := c.Container.ORM.User.
Query().
Where(user.Email(form.Email)).
Only(c.Request().Context())
Only(ctx.Request().Context())
switch err.(type) {
case *ent.NotFoundError:
return succeed()
case nil:
default:
return fail("error querying user during forgot password", err)
return c.Fail(ctx, err, "error querying user during forgot password")
}
// Generate the token
token, _, err := f.Container.Auth.GeneratePasswordResetToken(c, u.ID)
token, _, err := c.Container.Auth.GeneratePasswordResetToken(ctx, u.ID)
if err != nil {
return fail("error generating password reset token", err)
return c.Fail(ctx, err, "error generating password reset token")
}
c.Logger().Infof("generated password reset token for user %d", u.ID)
ctx.Logger().Infof("generated password reset token for user %d", u.ID)
// Email the user
// TODO: better email
err = f.Container.Mail.Send(c, u.Email, fmt.Sprintf("Go here to reset your password: %s", c.Echo().Reverse("reset_password", u.ID, token)))
body := fmt.Sprintf(
"Go here to reset your password: %s",
ctx.Echo().Reverse("reset_password", u.ID, token),
)
ctx.Logger().Info(body)
err = c.Container.Mail.Send(ctx, u.Email, body)
if err != nil {
return fail("error sending password reset email", err)
return c.Fail(ctx, err, "error sending password reset email")
}
return succeed()

View file

@ -18,8 +18,8 @@ type (
}
LoginForm struct {
Email string `form:"email" validate:"required,email" label:"Email address"`
Password string `form:"password" validate:"required" label:"Password"`
Email string `form:"email" validate:"required,email"`
Password string `form:"password" validate:"required"`
Submission controller.FormSubmission
}
)

View file

@ -15,79 +15,77 @@ type (
}
RegisterForm struct {
Name string `form:"name" validate:"required" label:"Name"`
Email string `form:"email" validate:"required,email" label:"Email address"`
Password string `form:"password" validate:"required" label:"Password"`
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password" label:"Confirm password"`
Name string `form:"name" validate:"required"`
Email string `form:"email" validate:"required,email"`
Password string `form:"password" validate:"required"`
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
Submission controller.FormSubmission
}
)
func (r *Register) Get(c echo.Context) error {
p := controller.NewPage(c)
p.Layout = "auth"
p.Name = "register"
p.Title = "Register"
p.Data = RegisterForm{}
func (c *Register) Get(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = "auth"
page.Name = "register"
page.Title = "Register"
page.Form = RegisterForm{}
if form := c.Get(context.FormKey); form != nil {
p.Data = form.(RegisterForm)
if form := ctx.Get(context.FormKey); form != nil {
page.Form = form.(*RegisterForm)
}
return r.RenderPage(c, p)
return c.RenderPage(ctx, page)
}
func (r *Register) Post(c echo.Context) error {
fail := func(message string, err error) error {
c.Logger().Errorf("%s: %v", message, err)
msg.Danger(c, "An error occurred. Please try again.")
return r.Get(c)
}
func (c *Register) Post(ctx echo.Context) error {
var form RegisterForm
ctx.Set(context.FormKey, &form)
// Parse the form values
var form RegisterForm
if err := c.Bind(&form); err != nil {
return fail("unable to parse form values", err)
if err := ctx.Bind(&form); err != nil {
return c.Fail(ctx, err, "unable to parse register form")
}
c.Set(context.FormKey, form)
// Validate the form
if err := c.Validate(form); err != nil {
r.SetValidationErrorMessages(c, err, form)
return r.Get(c)
if err := form.Submission.Process(ctx, form); err != nil {
return c.Fail(ctx, err, "unable to process form submission")
}
if form.Submission.HasErrors() {
return c.Get(ctx)
}
// Hash the password
pwHash, err := r.Container.Auth.HashPassword(form.Password)
pwHash, err := c.Container.Auth.HashPassword(form.Password)
if err != nil {
return fail("unable to hash password", err)
return c.Fail(ctx, err, "unable to hash password")
}
// Attempt creating the user
u, err := r.Container.ORM.User.
u, err := c.Container.ORM.User.
Create().
SetName(form.Name).
SetEmail(form.Email).
SetPassword(pwHash).
Save(c.Request().Context())
Save(ctx.Request().Context())
switch err.(type) {
case nil:
c.Logger().Infof("user created: %s", u.Name)
ctx.Logger().Infof("user created: %s", u.Name)
case *ent.ConstraintError:
msg.Warning(c, "A user with this email address already exists. Please log in.")
return r.Redirect(c, "login")
msg.Warning(ctx, "A user with this email address already exists. Please log in.")
return c.Redirect(ctx, "login")
default:
return fail("unable to create user", err)
return c.Fail(ctx, err, "unable to create user")
}
// Log the user in
err = r.Container.Auth.Login(c, u.ID)
err = c.Container.Auth.Login(ctx, u.ID)
if err != nil {
c.Logger().Errorf("unable to log in: %v", err)
msg.Info(c, "Your account has been created.")
return r.Redirect(c, "login")
ctx.Logger().Errorf("unable to log in: %v", err)
msg.Info(ctx, "Your account has been created.")
return c.Redirect(ctx, "login")
}
msg.Info(c, "Your account has been created. You are now logged in.")
return r.Redirect(c, "home")
msg.Info(ctx, "Your account has been created. You are now logged in.")
return c.Redirect(ctx, "home")
}

View file

@ -16,64 +16,69 @@ type (
}
ResetPasswordForm struct {
Password string `form:"password" validate:"required" label:"Password"`
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password" label:"Confirm password"`
Password string `form:"password" validate:"required"`
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
Submission controller.FormSubmission
}
)
func (r *ResetPassword) Get(c echo.Context) error {
p := controller.NewPage(c)
p.Layout = "auth"
p.Name = "reset-password"
p.Title = "Reset password"
return r.RenderPage(c, p)
func (c *ResetPassword) Get(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = "auth"
page.Name = "reset-password"
page.Title = "Reset password"
page.Form = ResetPasswordForm{}
if form := ctx.Get(context.FormKey); form != nil {
page.Form = form.(*ResetPasswordForm)
}
return c.RenderPage(ctx, page)
}
func (r *ResetPassword) Post(c echo.Context) error {
fail := func(message string, err error) error {
c.Logger().Errorf("%s: %v", message, err)
msg.Danger(c, "An error occurred. Please try again.")
return r.Get(c)
}
func (c *ResetPassword) Post(ctx echo.Context) error {
var form ResetPasswordForm
ctx.Set(context.FormKey, &form)
// Parse the form values
var form ResetPasswordForm
if err := c.Bind(&form); err != nil {
return fail("unable to parse forgot password form", err)
if err := ctx.Bind(&form); err != nil {
return c.Fail(ctx, err, "unable to parse password reset form")
}
// Validate the form
if err := c.Validate(form); err != nil {
r.SetValidationErrorMessages(c, err, form)
return r.Get(c)
if err := form.Submission.Process(ctx, form); err != nil {
return c.Fail(ctx, err, "unable to process form submission")
}
if form.Submission.HasErrors() {
return c.Get(ctx)
}
// Hash the new password
hash, err := r.Container.Auth.HashPassword(form.Password)
hash, err := c.Container.Auth.HashPassword(form.Password)
if err != nil {
return fail("unable to hash password", err)
return c.Fail(ctx, err, "unable to hash password")
}
// Get the requesting user
usr := c.Get(context.UserKey).(*ent.User)
usr := ctx.Get(context.UserKey).(*ent.User)
// Update the user
_, err = r.Container.ORM.User.
_, err = c.Container.ORM.User.
Update().
SetPassword(hash).
Where(user.ID(usr.ID)).
Save(c.Request().Context())
Save(ctx.Request().Context())
if err != nil {
return fail("unable to update password", err)
return c.Fail(ctx, err, "unable to update password")
}
// Delete all password tokens for this user
err = r.Container.Auth.DeletePasswordTokens(c, usr.ID)
err = c.Container.Auth.DeletePasswordTokens(ctx, usr.ID)
if err != nil {
return fail("unable to delete password tokens", err)
return c.Fail(ctx, err, "unable to delete password tokens")
}
msg.Success(c, "Your password has been updated.")
return r.Redirect(c, "login")
msg.Success(ctx, "Your password has been updated.")
return c.Redirect(ctx, "login")
}