saasitone/routes/forgot_password.go

59 lines
1.2 KiB
Go
Raw Normal View History

2021-12-14 18:59:56 -08:00
package routes
import (
2021-12-14 19:14:39 -08:00
"goweb/context"
2021-12-14 18:59:56 -08:00
"goweb/controller"
"goweb/msg"
"github.com/labstack/echo/v4"
)
type (
ForgotPassword struct {
controller.Controller
}
ForgotPasswordForm struct {
Email string `form:"email" validate:"required,email" label:"Email address"`
}
)
func (f *ForgotPassword) Get(c echo.Context) error {
p := controller.NewPage(c)
p.Layout = "auth"
p.Name = "forgot-password"
p.Title = "Forgot password"
2021-12-14 19:14:39 -08:00
p.Data = ForgotPasswordForm{}
if form := c.Get(context.FormKey); form != nil {
p.Data = form.(ForgotPasswordForm)
}
2021-12-14 18:59:56 -08:00
return f.RenderPage(c, p)
}
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)
}
// Parse the form values
2021-12-14 19:14:39 -08:00
form := new(ForgotPasswordForm)
if err := c.Bind(form); err != nil {
2021-12-14 18:59:56 -08:00
return fail("unable to parse forgot password form", err)
}
2021-12-14 19:14:39 -08:00
c.Set(context.FormKey, *form)
2021-12-14 18:59:56 -08:00
// Validate the form
2021-12-14 19:14:39 -08:00
if err := c.Validate(form); err != nil {
f.SetValidationErrorMessages(c, err, form)
2021-12-14 18:59:56 -08:00
return f.Get(c)
}
2021-12-14 19:14:39 -08:00
// TODO: generate and email a token
2021-12-14 18:59:56 -08:00
return f.Redirect(c, "home")
}