Files
projectreshoot/internal/handler/account.go

178 lines
4.6 KiB
Go

package handler
import (
"context"
"database/sql"
"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"
"git.haelnorr.com/h/golib/cookies"
"github.com/pkg/errors"
)
// Renders the account page on the 'General' subpage
func AccountPage() http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("subpage")
subpage := "General"
if err == nil {
subpage = cookie.Value
}
page.Account(subpage).Render(r.Context(), w)
},
)
}
// Handles a request to change the subpage for the Accou/accountnt page
func AccountSubpage() http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
subpage := r.FormValue("subpage")
cookies.SetCookie(w, "subpage", "/account", subpage, 300)
account.AccountContainer(subpage).Render(r.Context(), w)
},
)
}
// Handles a request to change the users username
func ChangeUsername(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating username", err))
return
}
r.ParseForm()
newUsername := r.FormValue("username")
unique, err := models.CheckUsernameUnique(tx, newUsername)
if err != nil {
tx.Rollback()
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating username", err))
return
}
if !unique {
tx.Rollback()
account.ChangeUsername("Username is taken", newUsername).
Render(r.Context(), w)
return
}
user := auth.CurrentModel(r.Context())
err = user.ChangeUsername(tx, newUsername)
if err != nil {
tx.Rollback()
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating username", err))
return
}
tx.Commit()
w.Header().Set("HX-Refresh", "true")
},
)
}
// Handles a request to change the users bio
func ChangeBio(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating bio", err))
return
}
r.ParseForm()
newBio := r.FormValue("bio")
leng := len([]rune(newBio))
if leng > 128 {
tx.Rollback()
account.ChangeBio("Bio limited to 128 characters", newBio).
Render(r.Context(), w)
return
}
user := auth.CurrentModel(r.Context())
err = user.ChangeBio(tx, newBio)
if err != nil {
tx.Rollback()
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating bio", err))
return
}
tx.Commit()
w.Header().Set("HX-Refresh", "true")
},
)
}
func validateChangePassword(
r *http.Request,
) (string, error) {
r.ParseForm()
formPassword := r.FormValue("password")
formConfirmPassword := r.FormValue("confirm-password")
if formPassword != formConfirmPassword {
return "", errors.New("Passwords do not match")
}
if len(formPassword) > 72 {
return "", errors.New("Password exceeds maximum length of 72 bytes")
}
return formPassword, nil
}
// Handles a request to change the users password
func ChangePassword(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating password", err))
return
}
newPass, err := validateChangePassword(r)
if err != nil {
tx.Rollback()
account.ChangePassword(err.Error()).Render(r.Context(), w)
return
}
user := auth.CurrentModel(r.Context())
err = user.SetPassword(tx, newPass)
if err != nil {
tx.Rollback()
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating password", err))
return
}
tx.Commit()
w.Header().Set("HX-Refresh", "true")
},
)
}