Removed form data from route structs.

This commit is contained in:
mikestefanello 2021-12-14 22:14:39 -05:00
parent 0546a1b089
commit 1ac68b7d9f
4 changed files with 43 additions and 21 deletions

View file

@ -2,4 +2,5 @@ package context
const ( const (
AuthenticatedUserKey = "auth_user" AuthenticatedUserKey = "auth_user"
FormKey = "form"
) )

View file

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"goweb/context"
"goweb/controller" "goweb/controller"
"goweb/msg" "goweb/msg"
@ -10,7 +11,6 @@ import (
type ( type (
ForgotPassword struct { ForgotPassword struct {
controller.Controller controller.Controller
form ForgotPasswordForm
} }
ForgotPasswordForm struct { ForgotPasswordForm struct {
@ -23,7 +23,12 @@ func (f *ForgotPassword) Get(c echo.Context) error {
p.Layout = "auth" p.Layout = "auth"
p.Name = "forgot-password" p.Name = "forgot-password"
p.Title = "Forgot password" p.Title = "Forgot password"
p.Data = f.form p.Data = ForgotPasswordForm{}
if form := c.Get(context.FormKey); form != nil {
p.Data = form.(ForgotPasswordForm)
}
return f.RenderPage(c, p) return f.RenderPage(c, p)
} }
@ -35,16 +40,19 @@ func (f *ForgotPassword) Post(c echo.Context) error {
} }
// Parse the form values // Parse the form values
if err := c.Bind(&f.form); err != nil { form := new(ForgotPasswordForm)
if err := c.Bind(form); err != nil {
return fail("unable to parse forgot password form", err) return fail("unable to parse forgot password form", err)
} }
c.Set(context.FormKey, *form)
// Validate the form // Validate the form
if err := c.Validate(f.form); err != nil { if err := c.Validate(form); err != nil {
f.SetValidationErrorMessages(c, err, f.form) f.SetValidationErrorMessages(c, err, form)
return f.Get(c) return f.Get(c)
} }
// TODO // TODO: generate and email a token
return f.Redirect(c, "home") return f.Redirect(c, "home")
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"goweb/auth" "goweb/auth"
"goweb/context"
"goweb/controller" "goweb/controller"
"goweb/ent" "goweb/ent"
"goweb/ent/user" "goweb/ent/user"
@ -15,7 +16,6 @@ import (
type ( type (
Login struct { Login struct {
controller.Controller controller.Controller
form LoginForm
} }
LoginForm struct { LoginForm struct {
@ -29,7 +29,12 @@ func (l *Login) Get(c echo.Context) error {
p.Layout = "auth" p.Layout = "auth"
p.Name = "login" p.Name = "login"
p.Title = "Log in" p.Title = "Log in"
p.Data = l.form p.Data = LoginForm{}
if form := c.Get(context.FormKey); form != nil {
p.Data = form.(LoginForm)
}
return l.RenderPage(c, p) return l.RenderPage(c, p)
} }
@ -41,20 +46,22 @@ func (l *Login) Post(c echo.Context) error {
} }
// Parse the form values // Parse the form values
if err := c.Bind(&l.form); err != nil { form := new(LoginForm)
if err := c.Bind(form); err != nil {
return fail("unable to parse login form", err) return fail("unable to parse login form", err)
} }
c.Set(context.FormKey, *form)
// Validate the form // Validate the form
if err := c.Validate(l.form); err != nil { if err := c.Validate(form); err != nil {
l.SetValidationErrorMessages(c, err, l.form) l.SetValidationErrorMessages(c, err, form)
return l.Get(c) return l.Get(c)
} }
// Attempt to load the user // Attempt to load the user
u, err := l.Container.ORM.User. u, err := l.Container.ORM.User.
Query(). Query().
Where(user.Email(l.form.Email)). Where(user.Email(form.Email)).
First(c.Request().Context()) First(c.Request().Context())
if err != nil { if err != nil {
@ -68,7 +75,7 @@ func (l *Login) Post(c echo.Context) error {
} }
// Check if the password is correct // Check if the password is correct
err = auth.CheckPassword(l.form.Password, u.Password) err = auth.CheckPassword(form.Password, u.Password)
if err != nil { if err != nil {
msg.Danger(c, "Invalid credentials. Please try again.") msg.Danger(c, "Invalid credentials. Please try again.")
return l.Get(c) return l.Get(c)

View file

@ -2,6 +2,7 @@ package routes
import ( import (
"goweb/auth" "goweb/auth"
"goweb/context"
"goweb/controller" "goweb/controller"
"goweb/msg" "goweb/msg"
@ -11,7 +12,6 @@ import (
type ( type (
Register struct { Register struct {
controller.Controller controller.Controller
form RegisterForm
} }
RegisterForm struct { RegisterForm struct {
@ -27,7 +27,11 @@ func (r *Register) Get(c echo.Context) error {
p.Layout = "auth" p.Layout = "auth"
p.Name = "register" p.Name = "register"
p.Title = "Register" p.Title = "Register"
p.Data = r.form p.Data = RegisterForm{}
if form := c.Get(context.FormKey); form != nil {
p.Data = form.(RegisterForm)
}
return r.RenderPage(c, p) return r.RenderPage(c, p)
} }
@ -40,18 +44,20 @@ func (r *Register) Post(c echo.Context) error {
} }
// Parse the form values // Parse the form values
if err := c.Bind(&r.form); err != nil { form := new(RegisterForm)
if err := c.Bind(form); err != nil {
return fail("unable to parse form values", err) return fail("unable to parse form values", err)
} }
c.Set(context.FormKey, *form)
// Validate the form // Validate the form
if err := c.Validate(r.form); err != nil { if err := c.Validate(form); err != nil {
r.SetValidationErrorMessages(c, err, r.form) r.SetValidationErrorMessages(c, err, form)
return r.Get(c) return r.Get(c)
} }
// Hash the password // Hash the password
pwHash, err := auth.HashPassword(r.form.Password) pwHash, err := auth.HashPassword(form.Password)
if err != nil { if err != nil {
return fail("unable to hash password", err) return fail("unable to hash password", err)
} }
@ -59,8 +65,8 @@ func (r *Register) Post(c echo.Context) error {
// Attempt creating the user // Attempt creating the user
u, err := r.Container.ORM.User. u, err := r.Container.ORM.User.
Create(). Create().
SetName(r.form.Name). SetName(form.Name).
SetEmail(r.form.Email). SetEmail(form.Email).
SetPassword(pwHash). SetPassword(pwHash).
Save(c.Request().Context()) Save(c.Request().Context())