This commit is contained in:
@@ -56,7 +56,16 @@ func NewWebUI(a *auth.Store, tp *db.TrackpointStore, st *db.StopStore, j *db.Jou
|
||||
return &WebUI{authStore: a, tpStore: tp, stopStore: st, journalStore: j, userStore: u}
|
||||
}
|
||||
|
||||
func render(w http.ResponseWriter, page string, data any) {
|
||||
func injectNav(r *http.Request, data map[string]any) {
|
||||
user := userFromContext(r.Context())
|
||||
if user.UserID != "" {
|
||||
data["LoggedIn"] = true
|
||||
data["IsAdmin"] = user.IsAdmin
|
||||
}
|
||||
}
|
||||
|
||||
func render(w http.ResponseWriter, r *http.Request, page string, data map[string]any) {
|
||||
injectNav(r, data)
|
||||
t, err := tmpls.Clone()
|
||||
if err == nil {
|
||||
_, err = t.ParseFS(assets, "templates/"+page)
|
||||
@@ -120,7 +129,7 @@ func renderAdmin(w http.ResponseWriter, page string, data any) {
|
||||
// --- Auth ---
|
||||
|
||||
func (ui *WebUI) HandleGetLogin(w http.ResponseWriter, r *http.Request) {
|
||||
render(w, "login.html", map[string]any{"Error": "", "Username": ""})
|
||||
render(w, r, "login.html", map[string]any{"Error": "", "Username": ""})
|
||||
}
|
||||
|
||||
func (ui *WebUI) HandlePostLogin(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -137,7 +146,7 @@ func (ui *WebUI) HandlePostLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, auth.ErrInvalidCredentials) {
|
||||
msg = "Ungültige Zugangsdaten."
|
||||
}
|
||||
render(w, "login.html", map[string]any{"Error": msg, "Username": username})
|
||||
render(w, r, "login.html", map[string]any{"Error": msg, "Username": username})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -171,7 +180,7 @@ func (ui *WebUI) HandleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
// --- Register ---
|
||||
|
||||
func (ui *WebUI) HandleGetRegister(w http.ResponseWriter, r *http.Request) {
|
||||
render(w, "register.html", map[string]any{"Error": "", "Username": ""})
|
||||
render(w, r, "register.html", map[string]any{"Error": "", "Username": ""})
|
||||
}
|
||||
|
||||
func (ui *WebUI) HandlePostRegister(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -184,11 +193,11 @@ func (ui *WebUI) HandlePostRegister(w http.ResponseWriter, r *http.Request) {
|
||||
confirm := r.FormValue("confirm")
|
||||
|
||||
if username == "" || password == "" {
|
||||
render(w, "register.html", map[string]any{"Error": "Benutzername und Passwort sind Pflichtfelder.", "Username": username})
|
||||
render(w, r, "register.html", map[string]any{"Error": "Benutzername und Passwort sind Pflichtfelder.", "Username": username})
|
||||
return
|
||||
}
|
||||
if password != confirm {
|
||||
render(w, "register.html", map[string]any{"Error": "Passwörter stimmen nicht überein.", "Username": username})
|
||||
render(w, r, "register.html", map[string]any{"Error": "Passwörter stimmen nicht überein.", "Username": username})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -197,7 +206,7 @@ func (ui *WebUI) HandlePostRegister(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, auth.ErrUsernameTaken) {
|
||||
msg = "Benutzername bereits vergeben."
|
||||
}
|
||||
render(w, "register.html", map[string]any{"Error": msg, "Username": username})
|
||||
render(w, r, "register.html", map[string]any{"Error": msg, "Username": username})
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
@@ -211,7 +220,7 @@ func (ui *WebUI) HandleFeed(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Fehler beim Laden", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
render(w, "public.html", map[string]any{
|
||||
render(w, r, "public.html", map[string]any{
|
||||
"Entries": entries,
|
||||
"Offset": 20,
|
||||
"HasMore": len(entries) == 20,
|
||||
@@ -260,7 +269,7 @@ func (ui *WebUI) HandleDaysList(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Fehler beim Laden", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
render(w, "days.html", map[string]any{"Days": days, "IsAdmin": user.IsAdmin})
|
||||
render(w, r, "days.html", map[string]any{"Days": days})
|
||||
}
|
||||
|
||||
func (ui *WebUI) HandleDayDetail(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -288,7 +297,7 @@ func (ui *WebUI) HandleDayDetail(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
render(w, "day.html", map[string]any{
|
||||
render(w, r, "day.html", map[string]any{
|
||||
"Date": date,
|
||||
"Points": points,
|
||||
"Stops": stops,
|
||||
|
||||
Reference in New Issue
Block a user