Add change password feature
This commit is contained in:
parent
515142c055
commit
b9795be745
10 changed files with 228 additions and 96 deletions
|
|
@ -3,6 +3,8 @@ package auth
|
|||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
|
||||
|
|
@ -22,3 +24,80 @@ func (auth *Auth) authHome(ctx context.Context) http.Handler {
|
|||
Template: homeTemplate,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
//go:embed "templates/password.html"
|
||||
var passwordHTMLString string
|
||||
var passwordTemplate = static.AssetsAuthLogin.MustParseShared("password.html", passwordHTMLString)
|
||||
|
||||
type authpasswordContext struct {
|
||||
Message string
|
||||
Form template.HTML
|
||||
}
|
||||
|
||||
var (
|
||||
errPasswordsNotIdentical = errors.New("passwords are not identical")
|
||||
errPasswordIsEmpty = errors.New("password is empty")
|
||||
errPasswordIncorrect = errors.New("old password is not correct")
|
||||
errPasswordSetFailure = errors.New("error saving new password")
|
||||
errPasswordSet = errors.New("password was updated")
|
||||
)
|
||||
|
||||
func (auth *Auth) authPassword(ctx context.Context) http.Handler {
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []httpx.Field{
|
||||
{Name: "old", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
||||
{Name: "new", Type: httpx.PasswordField, EmptyOnError: true, Label: "New Password"},
|
||||
{Name: "new2", Type: httpx.PasswordField, EmptyOnError: true, Label: "New Password (again)"},
|
||||
},
|
||||
FieldTemplate: httpx.PureCSSFieldTemplate,
|
||||
|
||||
CSRF: auth.csrf.Get(nil),
|
||||
|
||||
RenderForm: func(template template.HTML, err error, w http.ResponseWriter, r *http.Request) {
|
||||
ctx := authpasswordContext{
|
||||
Message: "",
|
||||
Form: template,
|
||||
}
|
||||
if err != nil {
|
||||
ctx.Message = err.Error()
|
||||
}
|
||||
httpx.WriteHTML(ctx, nil, passwordTemplate, "", w, r)
|
||||
},
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
old, new, new2 := values["old"], values["new"], values["new2"]
|
||||
|
||||
if new != new2 {
|
||||
return struct{}{}, errPasswordsNotIdentical
|
||||
}
|
||||
|
||||
if new == "" {
|
||||
return struct{}{}, errPasswordIsEmpty
|
||||
}
|
||||
|
||||
user, err := auth.UserOf(r)
|
||||
if err != nil {
|
||||
return struct{}{}, err
|
||||
}
|
||||
|
||||
{
|
||||
err := user.CheckPassword(r.Context(), []byte(old))
|
||||
if err != nil {
|
||||
return struct{}{}, errPasswordIncorrect
|
||||
}
|
||||
}
|
||||
{
|
||||
err := user.SetPassword(r.Context(), []byte(new))
|
||||
if err != nil {
|
||||
return struct{}{}, errPasswordSetFailure
|
||||
}
|
||||
}
|
||||
|
||||
return struct{}{}, nil
|
||||
},
|
||||
|
||||
RenderSuccess: func(_ struct{}, values map[string]string, w http.ResponseWriter, r *http.Request) error {
|
||||
return errPasswordSet
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue