From 88fa53fb9e2f45f47b33d5edef43e7338d5c4f03 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Thu, 17 Oct 2024 21:58:02 +0200 Subject: Introduce change password functionality --- form/errors.go | 12 ++++++++ main.go | 1 + model/users.sql.go | 39 +++++++++++++++++++++++ pages/base.templ | 4 +++ pages/base_templ.go | 4 +-- pages/chpw.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ pages/chpw.templ | 25 +++++++++++++++ pages/chpw_templ.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ pages/login.go | 33 ++++++++++++++------ sql/users.sql | 12 +++++++- 10 files changed, 290 insertions(+), 13 deletions(-) create mode 100644 pages/chpw.go create mode 100644 pages/chpw.templ create mode 100644 pages/chpw_templ.go diff --git a/form/errors.go b/form/errors.go index 52206c4..ab9abd4 100644 --- a/form/errors.go +++ b/form/errors.go @@ -5,6 +5,18 @@ import ( "fmt" ) +type FormErrors struct { + Errors []error `form:"-"` +} + +func (f *FormErrors) AddError(field, issue string) { + f.Errors = append(f.Errors, FieldError{field, issue}) +} + +func (f *FormErrors) HasError() bool { + return len(f.Errors) > 0 +} + type FieldError struct { Field string Issue string diff --git a/main.go b/main.go index 2eadfd9..352ed56 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,7 @@ func main() { authRouter.Mount("/", pages.Init()) authRouter.Mount("/recur", pages.Recur()) authRouter.Mount("/categories", pages.Categories()) + authRouter.Mount("/cpw", pages.ChangePassword()) authRouter.NotFound(pages.NotFound()) log.Fatal(http.ListenAndServe(os.Getenv("GOSTEN_ADDRESS"), router)) diff --git a/model/users.sql.go b/model/users.sql.go index e63dacc..bd881b7 100644 --- a/model/users.sql.go +++ b/model/users.sql.go @@ -9,6 +9,24 @@ import ( "context" ) +const getPwdById = `-- name: GetPwdById :one + SELECT pwd + FROM users + WHERE id = $1 +` + +// GetPwdById +// +// SELECT pwd +// FROM users +// WHERE id = $1 +func (q *Queries) GetPwdById(ctx context.Context, id int32) (string, error) { + row := q.db.QueryRow(ctx, getPwdById, id) + var pwd string + err := row.Scan(&pwd) + return pwd, err +} + const getUserById = `-- name: GetUserById :one SELECT id, name, pwd, description FROM users @@ -88,3 +106,24 @@ func (q *Queries) GetUsers(ctx context.Context) ([]User, error) { } return items, nil } + +const updatePwd = `-- name: UpdatePwd :exec + UPDATE users + SET pwd = $1 + WHERE id = $2 +` + +type UpdatePwdParams struct { + Pwd string + ID int32 +} + +// UpdatePwd +// +// UPDATE users +// SET pwd = $1 +// WHERE id = $2 +func (q *Queries) UpdatePwd(ctx context.Context, arg UpdatePwdParams) error { + _, err := q.db.Exec(ctx, updatePwd, arg.Pwd, arg.ID) + return err +} diff --git a/pages/base.templ b/pages/base.templ index 33edd65..47de938 100644 --- a/pages/base.templ +++ b/pages/base.templ @@ -43,6 +43,7 @@ templ navlinks() { templ userlinks() {
  • +
  • Passwort Ändern
  • Logout
  • } @@ -55,6 +56,9 @@ templ navbar() { + + + /* The Navbar */ diff --git a/pages/base_templ.go b/pages/base_templ.go index d871263..f355b61 100644 --- a/pages/base_templ.go +++ b/pages/base_templ.go @@ -236,7 +236,7 @@ func userlinks() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • Logout
  • ") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • Passwort Ändern
  • Logout
  • ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -265,7 +265,7 @@ func navbar() templ.Component { templ_7745c5c3_Var11 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("