2021-12-16 04:29:16 -08:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-01-01 07:44:18 -08:00
|
|
|
"github.com/mikestefanello/pagoda/context"
|
|
|
|
"github.com/mikestefanello/pagoda/controller"
|
|
|
|
"github.com/mikestefanello/pagoda/ent"
|
|
|
|
"github.com/mikestefanello/pagoda/msg"
|
2021-12-16 04:29:16 -08:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
ResetPassword struct {
|
|
|
|
controller.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetPasswordForm struct {
|
2021-12-24 05:42:42 -08:00
|
|
|
Password string `form:"password" validate:"required"`
|
|
|
|
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
|
|
|
|
Submission controller.FormSubmission
|
2021-12-16 04:29:16 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-12-24 05:42:42 -08:00
|
|
|
func (c *ResetPassword) Get(ctx echo.Context) error {
|
|
|
|
page := controller.NewPage(ctx)
|
|
|
|
page.Layout = "auth"
|
|
|
|
page.Name = "reset-password"
|
|
|
|
page.Title = "Reset password"
|
|
|
|
page.Form = ResetPasswordForm{}
|
2021-12-16 04:29:16 -08:00
|
|
|
|
2021-12-24 05:42:42 -08:00
|
|
|
if form := ctx.Get(context.FormKey); form != nil {
|
|
|
|
page.Form = form.(*ResetPasswordForm)
|
2021-12-16 04:29:16 -08:00
|
|
|
}
|
|
|
|
|
2021-12-24 05:42:42 -08:00
|
|
|
return c.RenderPage(ctx, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ResetPassword) Post(ctx echo.Context) error {
|
2021-12-22 18:51:18 -08:00
|
|
|
var form ResetPasswordForm
|
2021-12-24 05:42:42 -08:00
|
|
|
ctx.Set(context.FormKey, &form)
|
|
|
|
|
|
|
|
// Parse the form values
|
|
|
|
if err := ctx.Bind(&form); err != nil {
|
|
|
|
return c.Fail(ctx, err, "unable to parse password reset form")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := form.Submission.Process(ctx, form); err != nil {
|
|
|
|
return c.Fail(ctx, err, "unable to process form submission")
|
2021-12-16 04:29:16 -08:00
|
|
|
}
|
|
|
|
|
2021-12-24 05:42:42 -08:00
|
|
|
if form.Submission.HasErrors() {
|
|
|
|
return c.Get(ctx)
|
2021-12-16 04:29:16 -08:00
|
|
|
}
|
|
|
|
|
2021-12-16 18:27:52 -08:00
|
|
|
// Hash the new password
|
2021-12-24 05:42:42 -08:00
|
|
|
hash, err := c.Container.Auth.HashPassword(form.Password)
|
2021-12-16 18:27:52 -08:00
|
|
|
if err != nil {
|
2021-12-24 05:42:42 -08:00
|
|
|
return c.Fail(ctx, err, "unable to hash password")
|
2021-12-16 18:27:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the requesting user
|
2021-12-24 05:42:42 -08:00
|
|
|
usr := ctx.Get(context.UserKey).(*ent.User)
|
2021-12-16 18:27:52 -08:00
|
|
|
|
|
|
|
// Update the user
|
2022-01-09 18:30:09 -08:00
|
|
|
_, err = usr.
|
2021-12-16 18:27:52 -08:00
|
|
|
Update().
|
|
|
|
SetPassword(hash).
|
2021-12-24 05:42:42 -08:00
|
|
|
Save(ctx.Request().Context())
|
2021-12-16 18:27:52 -08:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-24 05:42:42 -08:00
|
|
|
return c.Fail(ctx, err, "unable to update password")
|
2021-12-16 18:27:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all password tokens for this user
|
2021-12-24 05:42:42 -08:00
|
|
|
err = c.Container.Auth.DeletePasswordTokens(ctx, usr.ID)
|
2021-12-16 18:27:52 -08:00
|
|
|
if err != nil {
|
2021-12-24 05:42:42 -08:00
|
|
|
return c.Fail(ctx, err, "unable to delete password tokens")
|
2021-12-16 18:27:52 -08:00
|
|
|
}
|
2021-12-16 04:29:16 -08:00
|
|
|
|
2021-12-24 05:42:42 -08:00
|
|
|
msg.Success(ctx, "Your password has been updated.")
|
|
|
|
return c.Redirect(ctx, "login")
|
2021-12-16 04:29:16 -08:00
|
|
|
}
|