From 0546a1b0894062ef85f88617b8bb9aa1a7849333 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Tue, 14 Dec 2021 21:59:56 -0500 Subject: [PATCH] Added forgot password form. --- routes/forgot_password.go | 50 ++++++++++++++++++++++++++ routes/register.go | 6 ++-- routes/router.go | 4 +++ templates/pages/forgot-password.gohtml | 22 ++++++++++++ templates/pages/login.gohtml | 2 +- 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 routes/forgot_password.go create mode 100644 templates/pages/forgot-password.gohtml diff --git a/routes/forgot_password.go b/routes/forgot_password.go new file mode 100644 index 0000000..20ef6d9 --- /dev/null +++ b/routes/forgot_password.go @@ -0,0 +1,50 @@ +package routes + +import ( + "goweb/controller" + "goweb/msg" + + "github.com/labstack/echo/v4" +) + +type ( + ForgotPassword struct { + controller.Controller + form ForgotPasswordForm + } + + 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" + p.Data = f.form + 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 + if err := c.Bind(&f.form); err != nil { + return fail("unable to parse forgot password form", err) + } + + // Validate the form + if err := c.Validate(f.form); err != nil { + f.SetValidationErrorMessages(c, err, f.form) + return f.Get(c) + } + + // TODO + return f.Redirect(c, "home") +} diff --git a/routes/register.go b/routes/register.go index 8ab0f67..0913764 100644 --- a/routes/register.go +++ b/routes/register.go @@ -18,7 +18,7 @@ type ( 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"` // TODO validate same + ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password" label:"Confirm password"` } ) @@ -72,7 +72,9 @@ func (r *Register) Post(c echo.Context) error { err = auth.Login(c, u.ID) if err != nil { - // TODO + c.Logger().Errorf("unable to log in: %v", err) + msg.Info(c, "Your account has been created.") + return r.Redirect(c, "login") } msg.Info(c, "Your account has been created. You are now logged in.") diff --git a/routes/router.go b/routes/router.go index 01ddad7..617913c 100644 --- a/routes/router.go +++ b/routes/router.go @@ -99,4 +99,8 @@ func userRoutes(g *echo.Group, ctr controller.Controller) { register := Register{Controller: ctr} noAuth.GET("/register", register.Get).Name = "register" noAuth.POST("/register", register.Post).Name = "register.post" + + forgot := ForgotPassword{Controller: ctr} + noAuth.GET("/password", forgot.Get).Name = "forgot_password" + noAuth.POST("/password", forgot.Post).Name = "forgot_password.post" } diff --git a/templates/pages/forgot-password.gohtml b/templates/pages/forgot-password.gohtml new file mode 100644 index 0000000..e838e83 --- /dev/null +++ b/templates/pages/forgot-password.gohtml @@ -0,0 +1,22 @@ +{{define "content"}} +
+
+

Enter your email address and we'll email you a link that allows you to reset your password.

+
+
+ +
+ +
+
+
+

+ +

+

+ Cancel +

+
+ {{template "csrf" .}} +
+{{end}} \ No newline at end of file diff --git a/templates/pages/login.gohtml b/templates/pages/login.gohtml index 48c1369..d82ebf4 100644 --- a/templates/pages/login.gohtml +++ b/templates/pages/login.gohtml @@ -22,5 +22,5 @@ {{template "csrf" .}} -
Create an account
+
Create an account - Forgot password?
{{end}} \ No newline at end of file