Updated login form and controller.

This commit is contained in:
mikestefanello 2021-12-23 23:04:00 -05:00
parent 576caf217c
commit d5adf010db
6 changed files with 59 additions and 61 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"reflect" "reflect"
"goweb/htmx"
"goweb/middleware" "goweb/middleware"
"goweb/msg" "goweb/msg"
"goweb/services" "goweb/services"
@ -153,7 +154,11 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
// Redirect redirects to a given route name with optional route parameters // Redirect redirects to a given route name with optional route parameters
func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error { func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error {
return ctx.Redirect(http.StatusFound, ctx.Echo().Reverse(route, routeParams)) url := ctx.Echo().Reverse(route, routeParams)
h := htmx.Response{}
h.Redirect = url
h.Apply(ctx)
return ctx.Redirect(http.StatusFound, url)
} }
func (c *Controller) Fail(ctx echo.Context, err error, log string) error { func (c *Controller) Fail(ctx echo.Context, err error, log string) error {

View file

@ -1,8 +1,6 @@
package controller package controller
import ( import (
"reflect"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -78,21 +76,9 @@ func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
return return
} }
formType := reflect.TypeOf(form)
for _, ve := range ves { for _, ve := range ves {
var message string var message string
// Default the field form name to the name of the struct field
fieldName := ve.StructField()
// Attempt to get the form field name from the field's struct tag
if field, ok := formType.FieldByName(ve.Field()); ok {
if fieldNameTag := field.Tag.Get("form"); fieldNameTag != "" {
fieldName = fieldNameTag
}
}
// Provide better error messages depending on the failed validation tag // Provide better error messages depending on the failed validation tag
// This should be expanded as you use additional tags in your validation // This should be expanded as you use additional tags in your validation
switch ve.Tag() { switch ve.Tag() {
@ -107,6 +93,6 @@ func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
} }
// Add the error // Add the error
f.SetFieldError(fieldName, message) f.SetFieldError(ve.Field(), message)
} }
} }

View file

@ -27,15 +27,17 @@ func (c *Contact) Get(ctx echo.Context) error {
p.Form = ContactForm{} p.Form = ContactForm{}
if form := ctx.Get(context.FormKey); form != nil { if form := ctx.Get(context.FormKey); form != nil {
p.Form = form.(ContactForm) p.Form = form.(*ContactForm)
} }
return c.RenderPage(ctx, p) return c.RenderPage(ctx, p)
} }
func (c *Contact) Post(ctx echo.Context) error { func (c *Contact) Post(ctx echo.Context) error {
// Parse the form values
var form ContactForm var form ContactForm
ctx.Set(context.FormKey, &form)
// Parse the form values
if err := ctx.Bind(&form); err != nil { if err := ctx.Bind(&form); err != nil {
return c.Fail(ctx, err, "unable to bind form") return c.Fail(ctx, err, "unable to bind form")
} }
@ -44,7 +46,7 @@ func (c *Contact) Post(ctx echo.Context) error {
return c.Fail(ctx, err, "unable to process form submission") return c.Fail(ctx, err, "unable to process form submission")
} }
ctx.Set(context.FormKey, form) //ctx.Set(context.FormKey, form)
if !form.Submission.HasErrors() { if !form.Submission.HasErrors() {
if err := c.Container.Mail.Send(ctx, form.Email, "Hello!"); err != nil { if err := c.Container.Mail.Send(ctx, form.Email, "Hello!"); err != nil {

View file

@ -18,73 +18,76 @@ type (
} }
LoginForm struct { LoginForm struct {
Email string `form:"email" validate:"required,email" label:"Email address"` Email string `form:"email" validate:"required,email" label:"Email address"`
Password string `form:"password" validate:"required" label:"Password"` Password string `form:"password" validate:"required" label:"Password"`
Submission controller.FormSubmission
} }
) )
func (l *Login) Get(c echo.Context) error { func (c *Login) Get(ctx echo.Context) error {
p := controller.NewPage(c) page := controller.NewPage(ctx)
p.Layout = "auth" page.Layout = "auth"
p.Name = "login" page.Name = "login"
p.Title = "Log in" page.Title = "Log in"
p.Data = LoginForm{} page.Form = LoginForm{}
if form := c.Get(context.FormKey); form != nil { if form := ctx.Get(context.FormKey); form != nil {
p.Data = form.(LoginForm) page.Form = form.(*LoginForm)
} }
return l.RenderPage(c, p) return c.RenderPage(ctx, page)
} }
func (l *Login) Post(c echo.Context) error { func (c *Login) Post(ctx echo.Context) error {
fail := func(message string, err error) error { var form LoginForm
c.Logger().Errorf("%s: %v", message, err) ctx.Set(context.FormKey, &form)
msg.Danger(c, "An error occurred. Please try again.")
return l.Get(c) authFailed := func() error {
form.Submission.SetFieldError("Email", "")
form.Submission.SetFieldError("Password", "")
msg.Danger(ctx, "Invalid credentials. Please try again.")
return c.Get(ctx)
} }
// Parse the form values // Parse the form values
var form LoginForm if err := ctx.Bind(&form); err != nil {
if err := c.Bind(&form); err != nil { return c.Fail(ctx, err, "unable to parse login form")
return fail("unable to parse login form", err)
} }
c.Set(context.FormKey, form)
// Validate the form if err := form.Submission.Process(ctx, form); err != nil {
if err := c.Validate(form); err != nil { return c.Fail(ctx, err, "unable to process form submission")
l.SetValidationErrorMessages(c, err, form) }
return l.Get(c)
if form.Submission.HasErrors() {
return c.Get(ctx)
} }
// Attempt to load the user // Attempt to load the user
u, err := l.Container.ORM.User. u, err := c.Container.ORM.User.
Query(). Query().
Where(user.Email(form.Email)). Where(user.Email(form.Email)).
Only(c.Request().Context()) Only(ctx.Request().Context())
switch err.(type) { switch err.(type) {
case *ent.NotFoundError: case *ent.NotFoundError:
msg.Danger(c, "Invalid credentials. Please try again.") return authFailed()
return l.Get(c)
case nil: case nil:
default: default:
return fail("error querying user during login", err) return c.Fail(ctx, err, "error querying user during login")
} }
// Check if the password is correct // Check if the password is correct
err = l.Container.Auth.CheckPassword(form.Password, u.Password) err = c.Container.Auth.CheckPassword(form.Password, u.Password)
if err != nil { if err != nil {
msg.Danger(c, "Invalid credentials. Please try again.") return authFailed()
return l.Get(c)
} }
// Log the user in // Log the user in
err = l.Container.Auth.Login(c, u.ID) err = c.Container.Auth.Login(ctx, u.ID)
if err != nil { if err != nil {
return fail("unable to log in user", err) return c.Fail(ctx, err, "unable to log in user")
} }
msg.Success(c, fmt.Sprintf("Welcome back, %s. You are now logged in.", u.Name)) msg.Success(ctx, fmt.Sprintf("Welcome back, <strong>%s</strong>. You are now logged in.", u.Name))
return l.Redirect(c, "home") return c.Redirect(ctx, "home")
} }

View file

@ -26,17 +26,17 @@
<div class="field"> <div class="field">
<label for="email" class="label">Email address</label> <label for="email" class="label">Email address</label>
<div class="control"> <div class="control">
<input id="email" name="email" type="email" class="input {{.Form.Submission.GetFieldStatusClass "email"}}" value="{{.Form.Email}}"> <input id="email" name="email" type="email" class="input {{.Form.Submission.GetFieldStatusClass "Email"}}" value="{{.Form.Email}}">
</div> </div>
{{template "field-errors" (.Form.Submission.GetFieldErrors "email")}} {{template "field-errors" (.Form.Submission.GetFieldErrors "Email")}}
</div> </div>
<div class="field"> <div class="field">
<label for="message" class="label">Message</label> <label for="message" class="label">Message</label>
<div class="control"> <div class="control">
<textarea id="message" name="message" class="textarea {{.Form.Submission.GetFieldStatusClass "message"}}">{{.Form.Message}}</textarea> <textarea id="message" name="message" class="textarea {{.Form.Submission.GetFieldStatusClass "Message"}}">{{.Form.Message}}</textarea>
</div> </div>
{{template "field-errors" (.Form.Submission.GetFieldErrors "message")}} {{template "field-errors" (.Form.Submission.GetFieldErrors "Message")}}
</div> </div>
<div class="field is-grouped"> <div class="field is-grouped">

View file

@ -3,13 +3,15 @@
<div class="field"> <div class="field">
<label for="email" class="label">Email address</label> <label for="email" class="label">Email address</label>
<div class="control"> <div class="control">
<input id="email" type="email" name="email" class="input" value="{{.Data.Email}}" required> <input id="email" type="email" name="email" class="input {{.Form.Submission.GetFieldStatusClass "Email"}}" value="{{.Form.Email}}" required>
{{template "field-errors" (.Form.Submission.GetFieldErrors "Email")}}
</div> </div>
</div> </div>
<div class="field"> <div class="field">
<label for="password" class="label">Password</label> <label for="password" class="label">Password</label>
<div class="control"> <div class="control">
<input id="password" type="password" name="password" placeholder="*******" class="input" required> <input id="password" type="password" name="password" placeholder="*******" class="input {{.Form.Submission.GetFieldStatusClass "Password"}}" required>
{{template "field-errors" (.Form.Submission.GetFieldErrors "Password")}}
</div> </div>
</div> </div>
<div class="field is-grouped"> <div class="field is-grouped">