modularised webserver and auth systems

This commit is contained in:
2026-01-04 01:14:06 +11:00
parent 4a21ba3821
commit 28b7ba34f0
36 changed files with 451 additions and 774 deletions

View File

@@ -6,13 +6,13 @@ import (
"net/http"
"time"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
"projectreshoot/internal/models"
"projectreshoot/internal/view/component/account"
"projectreshoot/internal/view/page"
"projectreshoot/pkg/contexts"
"git.haelnorr.com/h/golib/cookies"
"git.haelnorr.com/h/golib/hlog"
"github.com/pkg/errors"
)
@@ -45,7 +45,8 @@ func AccountSubpage() http.Handler {
// Handles a request to change the users username
func ChangeUsername(
logger *hlog.Logger,
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
@@ -56,8 +57,7 @@ func ChangeUsername(
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
logger.Warn().Err(err).Msg("Error updating username")
w.WriteHeader(http.StatusServiceUnavailable)
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating username", err))
return
}
r.ParseForm()
@@ -65,8 +65,7 @@ func ChangeUsername(
unique, err := models.CheckUsernameUnique(tx, newUsername)
if err != nil {
tx.Rollback()
logger.Error().Err(err).Msg("Error updating username")
w.WriteHeader(http.StatusInternalServerError)
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating username", err))
return
}
if !unique {
@@ -75,12 +74,11 @@ func ChangeUsername(
Render(r.Context(), w)
return
}
user := contexts.GetUser(r.Context())
user := auth.CurrentModel(r.Context())
err = user.ChangeUsername(tx, newUsername)
if err != nil {
tx.Rollback()
logger.Error().Err(err).Msg("Error updating username")
w.WriteHeader(http.StatusInternalServerError)
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating username", err))
return
}
tx.Commit()
@@ -91,7 +89,8 @@ func ChangeUsername(
// Handles a request to change the users bio
func ChangeBio(
logger *hlog.Logger,
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
@@ -102,8 +101,7 @@ func ChangeBio(
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
logger.Warn().Err(err).Msg("Error updating bio")
w.WriteHeader(http.StatusServiceUnavailable)
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating bio", err))
return
}
r.ParseForm()
@@ -115,12 +113,11 @@ func ChangeBio(
Render(r.Context(), w)
return
}
user := contexts.GetUser(r.Context())
user := auth.CurrentModel(r.Context())
err = user.ChangeBio(tx, newBio)
if err != nil {
tx.Rollback()
logger.Error().Err(err).Msg("Error updating bio")
w.WriteHeader(http.StatusInternalServerError)
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating bio", err))
return
}
tx.Commit()
@@ -145,7 +142,8 @@ func validateChangePassword(
// Handles a request to change the users password
func ChangePassword(
logger *hlog.Logger,
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
@@ -156,8 +154,7 @@ func ChangePassword(
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
logger.Warn().Err(err).Msg("Error updating password")
w.WriteHeader(http.StatusServiceUnavailable)
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating password", err))
return
}
newPass, err := validateChangePassword(r)
@@ -166,12 +163,11 @@ func ChangePassword(
account.ChangePassword(err.Error()).Render(r.Context(), w)
return
}
user := contexts.GetUser(r.Context())
user := auth.CurrentModel(r.Context())
err = user.SetPassword(tx, newPass)
if err != nil {
tx.Rollback()
logger.Error().Err(err).Msg("Error updating password")
w.WriteHeader(http.StatusInternalServerError)
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating password", err))
return
}
tx.Commit()