Refactored all forms to follow new pattern.
This commit is contained in:
parent
d5adf010db
commit
6f50552a15
11 changed files with 133 additions and 195 deletions
|
|
@ -4,15 +4,10 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"goweb/htmx"
|
|
||||||
"goweb/middleware"
|
"goweb/middleware"
|
||||||
"goweb/msg"
|
|
||||||
"goweb/services"
|
"goweb/services"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
|
||||||
|
|
||||||
"github.com/eko/gocache/v2/marshaler"
|
"github.com/eko/gocache/v2/marshaler"
|
||||||
|
|
||||||
"github.com/eko/gocache/v2/store"
|
"github.com/eko/gocache/v2/store"
|
||||||
|
|
@ -155,57 +150,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 {
|
||||||
url := ctx.Echo().Reverse(route, routeParams)
|
url := ctx.Echo().Reverse(route, routeParams)
|
||||||
h := htmx.Response{}
|
// TODO: HTMX redirect?
|
||||||
h.Redirect = url
|
|
||||||
h.Apply(ctx)
|
|
||||||
return ctx.Redirect(http.StatusFound, url)
|
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 {
|
||||||
ctx.Logger().Errorf("%s: %v", log, err)
|
ctx.Logger().Errorf("%s: %v", log, err)
|
||||||
return echo.NewHTTPError(500)
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
}
|
|
||||||
|
|
||||||
// SetValidationErrorMessages sets error flash messages for validation failures of a given struct
|
|
||||||
// and attempts to provide more user-friendly wording.
|
|
||||||
// The error should result from the validator module and the data should be the struct that failed
|
|
||||||
// validation.
|
|
||||||
// This method supports including a struct tag of "labeL" on each field which will be the name
|
|
||||||
// of the field used in the error messages, for example:
|
|
||||||
// - FirstName string `form:"first-name" validate:"required" label:"First name"`
|
|
||||||
// Only a few validator tags are supported below. Expand them as needed.
|
|
||||||
func (c *Controller) SetValidationErrorMessages(ctx echo.Context, err error, data interface{}) {
|
|
||||||
ves, ok := err.(validator.ValidationErrors)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ve := range ves {
|
|
||||||
var message string
|
|
||||||
|
|
||||||
// Default the field label to the name of the struct field
|
|
||||||
label := ve.StructField()
|
|
||||||
|
|
||||||
// Attempt to get a label from the field's struct tag
|
|
||||||
if field, ok := reflect.TypeOf(data).FieldByName(ve.Field()); ok {
|
|
||||||
if labelTag := field.Tag.Get("label"); labelTag != "" {
|
|
||||||
label = labelTag
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Provide better error messages depending on the failed validation tag
|
|
||||||
// This should be expanded as you use additional tags in your validation
|
|
||||||
switch ve.Tag() {
|
|
||||||
case "required":
|
|
||||||
message = "%s is required."
|
|
||||||
case "email":
|
|
||||||
message = "%s must be a valid email address."
|
|
||||||
case "eqfield":
|
|
||||||
message = "%s must match."
|
|
||||||
default:
|
|
||||||
message = "%s is not a valid value."
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Danger(ctx, fmt.Sprintf(message, "<strong>"+label+"</strong>"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
|
|
||||||
"goweb/config"
|
"goweb/config"
|
||||||
"goweb/middleware"
|
"goweb/middleware"
|
||||||
"goweb/msg"
|
|
||||||
"goweb/services"
|
"goweb/services"
|
||||||
"goweb/tests"
|
"goweb/tests"
|
||||||
|
|
||||||
|
|
@ -18,8 +17,6 @@ import (
|
||||||
|
|
||||||
"github.com/eko/gocache/v2/marshaler"
|
"github.com/eko/gocache/v2/marshaler"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
|
@ -56,25 +53,6 @@ func TestController_Redirect(t *testing.T) {
|
||||||
assert.Equal(t, http.StatusFound, ctx.Response().Status)
|
assert.Equal(t, http.StatusFound, ctx.Response().Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestController_SetValidationErrorMessages(t *testing.T) {
|
|
||||||
type example struct {
|
|
||||||
Name string `validate:"required" label:"Label test"`
|
|
||||||
}
|
|
||||||
e := example{}
|
|
||||||
v := validator.New()
|
|
||||||
err := v.Struct(e)
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
ctx, _ := tests.NewContext(c.Web, "/")
|
|
||||||
tests.InitSession(ctx)
|
|
||||||
ctr := NewController(c)
|
|
||||||
ctr.SetValidationErrorMessages(ctx, err, e)
|
|
||||||
|
|
||||||
msgs := msg.Get(ctx, msg.TypeDanger)
|
|
||||||
require.Len(t, msgs, 1)
|
|
||||||
assert.Equal(t, "<strong>Label test</strong> is required.", msgs[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestController_RenderPage(t *testing.T) {
|
func TestController_RenderPage(t *testing.T) {
|
||||||
setup := func() (echo.Context, *httptest.ResponseRecorder, Controller, Page) {
|
setup := func() (echo.Context, *httptest.ResponseRecorder, Controller, Page) {
|
||||||
ctx, rec := tests.NewContext(c.Web, "/test/TestController_RenderPage")
|
ctx, rec := tests.NewContext(c.Web, "/test/TestController_RenderPage")
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ func GetRequest(ctx echo.Context) Request {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Apply(ctx echo.Context) {
|
func (r Response) Apply(ctx echo.Context) {
|
||||||
if r.Push != "" {
|
if r.Push != "" {
|
||||||
ctx.Response().Header().Set(HeaderPush, r.Push)
|
ctx.Response().Header().Set(HeaderPush, r.Push)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,6 @@ 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)
|
|
||||||
|
|
||||||
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 {
|
||||||
return c.Fail(ctx, err, "unable to send email")
|
return c.Fail(ctx, err, "unable to send email")
|
||||||
|
|
|
||||||
|
|
@ -18,76 +18,79 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
ForgotPasswordForm struct {
|
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 {
|
func (c *ForgotPassword) Get(ctx echo.Context) error {
|
||||||
p := controller.NewPage(c)
|
page := controller.NewPage(ctx)
|
||||||
p.Layout = "auth"
|
page.Layout = "auth"
|
||||||
p.Name = "forgot-password"
|
page.Name = "forgot-password"
|
||||||
p.Title = "Forgot password"
|
page.Title = "Forgot password"
|
||||||
p.Data = ForgotPasswordForm{}
|
page.Form = ForgotPasswordForm{}
|
||||||
|
|
||||||
if form := c.Get(context.FormKey); form != nil {
|
if form := ctx.Get(context.FormKey); form != nil {
|
||||||
p.Data = form.(ForgotPasswordForm)
|
page.Form = form.(*ForgotPasswordForm)
|
||||||
}
|
}
|
||||||
|
|
||||||
return f.RenderPage(c, p)
|
return c.RenderPage(ctx, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ForgotPassword) Post(c echo.Context) error {
|
func (c *ForgotPassword) Post(ctx echo.Context) error {
|
||||||
fail := func(message string, err error) error {
|
var form ForgotPasswordForm
|
||||||
c.Logger().Errorf("%s: %v", message, err)
|
ctx.Set(context.FormKey, &form)
|
||||||
msg.Danger(c, "An error occurred. Please try again.")
|
|
||||||
return f.Get(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
succeed := func() error {
|
succeed := func() error {
|
||||||
c.Set(context.FormKey, nil)
|
ctx.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.")
|
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 f.Get(c)
|
return c.Get(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the form values
|
// Parse the form values
|
||||||
var form ForgotPasswordForm
|
if err := ctx.Bind(&form); err != nil {
|
||||||
if err := c.Bind(&form); err != nil {
|
return c.Fail(ctx, err, "unable to parse forgot password form")
|
||||||
return fail("unable to parse forgot password 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")
|
||||||
f.SetValidationErrorMessages(c, err, form)
|
}
|
||||||
return f.Get(c)
|
|
||||||
|
if form.Submission.HasErrors() {
|
||||||
|
return c.Get(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to load the user
|
// Attempt to load the user
|
||||||
u, err := f.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:
|
||||||
return succeed()
|
return succeed()
|
||||||
case nil:
|
case nil:
|
||||||
default:
|
default:
|
||||||
return fail("error querying user during forgot password", err)
|
return c.Fail(ctx, err, "error querying user during forgot password")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the token
|
// Generate the token
|
||||||
token, _, err := f.Container.Auth.GeneratePasswordResetToken(c, u.ID)
|
token, _, err := c.Container.Auth.GeneratePasswordResetToken(ctx, u.ID)
|
||||||
if err != nil {
|
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
|
// Email the user
|
||||||
// TODO: better email
|
body := fmt.Sprintf(
|
||||||
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)))
|
"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 {
|
if err != nil {
|
||||||
return fail("error sending password reset email", err)
|
return c.Fail(ctx, err, "error sending password reset email")
|
||||||
}
|
}
|
||||||
|
|
||||||
return succeed()
|
return succeed()
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
LoginForm struct {
|
LoginForm struct {
|
||||||
Email string `form:"email" validate:"required,email" label:"Email address"`
|
Email string `form:"email" validate:"required,email"`
|
||||||
Password string `form:"password" validate:"required" label:"Password"`
|
Password string `form:"password" validate:"required"`
|
||||||
Submission controller.FormSubmission
|
Submission controller.FormSubmission
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -15,79 +15,77 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisterForm struct {
|
RegisterForm struct {
|
||||||
Name string `form:"name" validate:"required" label:"Name"`
|
Name string `form:"name" validate:"required"`
|
||||||
Email string `form:"email" validate:"required,email" label:"Email address"`
|
Email string `form:"email" validate:"required,email"`
|
||||||
Password string `form:"password" validate:"required" label:"Password"`
|
Password string `form:"password" validate:"required"`
|
||||||
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password" label:"Confirm password"`
|
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
|
||||||
|
Submission controller.FormSubmission
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *Register) Get(c echo.Context) error {
|
func (c *Register) Get(ctx echo.Context) error {
|
||||||
p := controller.NewPage(c)
|
page := controller.NewPage(ctx)
|
||||||
p.Layout = "auth"
|
page.Layout = "auth"
|
||||||
p.Name = "register"
|
page.Name = "register"
|
||||||
p.Title = "Register"
|
page.Title = "Register"
|
||||||
p.Data = RegisterForm{}
|
page.Form = RegisterForm{}
|
||||||
|
|
||||||
if form := c.Get(context.FormKey); form != nil {
|
if form := ctx.Get(context.FormKey); form != nil {
|
||||||
p.Data = form.(RegisterForm)
|
page.Form = form.(*RegisterForm)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.RenderPage(c, p)
|
return c.RenderPage(ctx, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Register) Post(c echo.Context) error {
|
func (c *Register) Post(ctx echo.Context) error {
|
||||||
fail := func(message string, err error) error {
|
var form RegisterForm
|
||||||
c.Logger().Errorf("%s: %v", message, err)
|
ctx.Set(context.FormKey, &form)
|
||||||
msg.Danger(c, "An error occurred. Please try again.")
|
|
||||||
return r.Get(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the form values
|
// Parse the form values
|
||||||
var form RegisterForm
|
if err := ctx.Bind(&form); err != nil {
|
||||||
if err := c.Bind(&form); err != nil {
|
return c.Fail(ctx, err, "unable to parse register form")
|
||||||
return fail("unable to parse form values", 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")
|
||||||
r.SetValidationErrorMessages(c, err, form)
|
}
|
||||||
return r.Get(c)
|
|
||||||
|
if form.Submission.HasErrors() {
|
||||||
|
return c.Get(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash the password
|
// Hash the password
|
||||||
pwHash, err := r.Container.Auth.HashPassword(form.Password)
|
pwHash, err := c.Container.Auth.HashPassword(form.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fail("unable to hash password", err)
|
return c.Fail(ctx, err, "unable to hash password")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt creating the user
|
// Attempt creating the user
|
||||||
u, err := r.Container.ORM.User.
|
u, err := c.Container.ORM.User.
|
||||||
Create().
|
Create().
|
||||||
SetName(form.Name).
|
SetName(form.Name).
|
||||||
SetEmail(form.Email).
|
SetEmail(form.Email).
|
||||||
SetPassword(pwHash).
|
SetPassword(pwHash).
|
||||||
Save(c.Request().Context())
|
Save(ctx.Request().Context())
|
||||||
|
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
c.Logger().Infof("user created: %s", u.Name)
|
ctx.Logger().Infof("user created: %s", u.Name)
|
||||||
case *ent.ConstraintError:
|
case *ent.ConstraintError:
|
||||||
msg.Warning(c, "A user with this email address already exists. Please log in.")
|
msg.Warning(ctx, "A user with this email address already exists. Please log in.")
|
||||||
return r.Redirect(c, "login")
|
return c.Redirect(ctx, "login")
|
||||||
default:
|
default:
|
||||||
return fail("unable to create user", err)
|
return c.Fail(ctx, err, "unable to create user")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the user in
|
// Log the user in
|
||||||
err = r.Container.Auth.Login(c, u.ID)
|
err = c.Container.Auth.Login(ctx, u.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger().Errorf("unable to log in: %v", err)
|
ctx.Logger().Errorf("unable to log in: %v", err)
|
||||||
msg.Info(c, "Your account has been created.")
|
msg.Info(ctx, "Your account has been created.")
|
||||||
return r.Redirect(c, "login")
|
return c.Redirect(ctx, "login")
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Info(c, "Your account has been created. You are now logged in.")
|
msg.Info(ctx, "Your account has been created. You are now logged in.")
|
||||||
return r.Redirect(c, "home")
|
return c.Redirect(ctx, "home")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,64 +16,69 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
ResetPasswordForm struct {
|
ResetPasswordForm struct {
|
||||||
Password string `form:"password" validate:"required" label:"Password"`
|
Password string `form:"password" validate:"required"`
|
||||||
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password" label:"Confirm password"`
|
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
|
||||||
|
Submission controller.FormSubmission
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *ResetPassword) Get(c echo.Context) error {
|
func (c *ResetPassword) Get(ctx echo.Context) error {
|
||||||
p := controller.NewPage(c)
|
page := controller.NewPage(ctx)
|
||||||
p.Layout = "auth"
|
page.Layout = "auth"
|
||||||
p.Name = "reset-password"
|
page.Name = "reset-password"
|
||||||
p.Title = "Reset password"
|
page.Title = "Reset password"
|
||||||
return r.RenderPage(c, p)
|
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 {
|
func (c *ResetPassword) Post(ctx echo.Context) error {
|
||||||
fail := func(message string, err error) error {
|
var form ResetPasswordForm
|
||||||
c.Logger().Errorf("%s: %v", message, err)
|
ctx.Set(context.FormKey, &form)
|
||||||
msg.Danger(c, "An error occurred. Please try again.")
|
|
||||||
return r.Get(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the form values
|
// Parse the form values
|
||||||
var form ResetPasswordForm
|
if err := ctx.Bind(&form); err != nil {
|
||||||
if err := c.Bind(&form); err != nil {
|
return c.Fail(ctx, err, "unable to parse password reset form")
|
||||||
return fail("unable to parse forgot password form", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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")
|
||||||
r.SetValidationErrorMessages(c, err, form)
|
}
|
||||||
return r.Get(c)
|
|
||||||
|
if form.Submission.HasErrors() {
|
||||||
|
return c.Get(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash the new password
|
// Hash the new password
|
||||||
hash, err := r.Container.Auth.HashPassword(form.Password)
|
hash, err := c.Container.Auth.HashPassword(form.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fail("unable to hash password", err)
|
return c.Fail(ctx, err, "unable to hash password")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the requesting user
|
// Get the requesting user
|
||||||
usr := c.Get(context.UserKey).(*ent.User)
|
usr := ctx.Get(context.UserKey).(*ent.User)
|
||||||
|
|
||||||
// Update the user
|
// Update the user
|
||||||
_, err = r.Container.ORM.User.
|
_, err = c.Container.ORM.User.
|
||||||
Update().
|
Update().
|
||||||
SetPassword(hash).
|
SetPassword(hash).
|
||||||
Where(user.ID(usr.ID)).
|
Where(user.ID(usr.ID)).
|
||||||
Save(c.Request().Context())
|
Save(ctx.Request().Context())
|
||||||
|
|
||||||
if err != nil {
|
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
|
// 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 {
|
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.")
|
msg.Success(ctx, "Your password has been updated.")
|
||||||
return r.Redirect(c, "login")
|
return c.Redirect(ctx, "login")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@
|
||||||
<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 is-grouped">
|
<div class="field is-grouped">
|
||||||
|
|
|
||||||
|
|
@ -3,25 +3,29 @@
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label for="name" class="label">Name</label>
|
<label for="name" class="label">Name</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<input type="text" id="name" name="name" class="input" value="{{.Data.Name}}" required>
|
<input type="text" id="name" name="name" class="input {{.Form.Submission.GetFieldStatusClass "Name"}}" value="{{.Form.Name}}" required>
|
||||||
|
{{template "field-errors" (.Form.Submission.GetFieldErrors "Name")}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<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 type="email" id="email" name="email" class="input" value="{{.Data.Email}}" required>
|
<input type="email" id="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 type="password" id="password" name="password" placeholder="*******" class="input" required>
|
<input type="password" id="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">
|
<div class="field">
|
||||||
<label for="password-confirm" class="label">Confirm password</label>
|
<label for="password-confirm" class="label">Confirm password</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<input type="password" id="password-confirm" name="password-confirm" placeholder="*******" class="input" required>
|
<input type="password" id="password-confirm" name="password-confirm" placeholder="*******" class="input {{.Form.Submission.GetFieldStatusClass "ConfirmPassword"}}" required>
|
||||||
|
{{template "field-errors" (.Form.Submission.GetFieldErrors "ConfirmPassword")}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="field is-grouped">
|
<div class="field is-grouped">
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,15 @@
|
||||||
<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 type="password" id="password" name="password" placeholder="*******" class="input" required>
|
<input type="password" id="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">
|
<div class="field">
|
||||||
<label for="password-confirm" class="label">Confirm password</label>
|
<label for="password-confirm" class="label">Confirm password</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<input type="password" id="password-confirm" name="password-confirm" placeholder="*******" class="input" required>
|
<input type="password" id="password-confirm" name="password-confirm" placeholder="*******" class="input {{.Form.Submission.GetFieldStatusClass "ConfirmPassword"}}" required>
|
||||||
|
{{template "field-errors" (.Form.Submission.GetFieldErrors "ConfirmPassword")}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="field is-grouped">
|
<div class="field is-grouped">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue