updated to use bun and updated hws modules.

This commit is contained in:
2026-01-11 23:39:10 +11:00
parent 6e03c98ae8
commit 1eedbc5220
33 changed files with 984 additions and 375 deletions

View File

@@ -2,35 +2,41 @@ package handler
import (
"context"
"database/sql"
"net/http"
"strings"
"time"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
"projectreshoot/internal/models"
"projectreshoot/internal/view/component/form"
"projectreshoot/internal/view/page"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
"git.haelnorr.com/h/golib/cookies"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
// Validates the username matches a user in the database and the password
// is correct. Returns the corresponding user
func validateLogin(
tx *sql.Tx,
ctx context.Context,
tx bun.Tx,
r *http.Request,
) (*models.User, error) {
) (*models.UserBun, error) {
formUsername := r.FormValue("username")
formPassword := r.FormValue("password")
user, err := models.GetUserFromUsername(tx, formUsername)
user, err := models.GetUserByUsername(ctx, tx, formUsername)
if err != nil {
return nil, errors.Wrap(err, "db.GetUserFromUsername")
}
err = user.CheckPassword(tx, formPassword)
if user == nil {
return nil, errors.New("Username or password incorrect")
}
err = user.CheckPassword(ctx, tx, formPassword)
if err != nil {
if !strings.Contains(err.Error(), "Username or password incorrect") {
return nil, errors.Wrap(err, "user.CheckPassword")
@@ -55,8 +61,8 @@ func checkRememberMe(r *http.Request) bool {
// template for user feedback
func LoginRequest(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
auth *hwsauth.Authenticator[*models.UserBun, bun.Tx],
db *bun.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
@@ -64,7 +70,7 @@ func LoginRequest(
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusServiceUnavailable,
@@ -77,7 +83,7 @@ func LoginRequest(
return
}
r.ParseForm()
user, err := validateLogin(tx, r)
user, err := validateLogin(ctx, tx, r)
if err != nil {
tx.Rollback()
if err.Error() != "Username or password incorrect" {