feat: Pass user to index template

This commit is contained in:
Leo 2026-08-18 08:04:46 +03:00
parent d2f2de8c50
commit 57ac5de1df
3 changed files with 9 additions and 5 deletions

View file

@ -49,7 +49,6 @@ func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
session, bump, err := h.auth.ValidateSessionToken(r.Context(), cookie.Value)
if err != nil {
log.Print(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
@ -63,6 +62,13 @@ func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, auth.NewSessionCookie(cookie.Value, false))
}
user, err := h.users.FindByID(r.Context(), session.UserID)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
files := []string{
"./ui/html/base.tmpl",
"./ui/html/pages/index.tmpl",
@ -75,7 +81,7 @@ func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
return
}
err = ts.ExecuteTemplate(w, "base", nil)
err = ts.ExecuteTemplate(w, "base", user.Username)
if err != nil {
log.Print(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)

View file

@ -20,8 +20,6 @@
</head>
<body>
<main>{{template "main" .}}</main>
<footer>Powered by <a href="https://go.dev/">Go</a></footer>
<!-- And include the JavaScript file -->
<script src="/static/js/main.js" type="text/javascript"></script>
</body>
</html>

View file

@ -1,6 +1,6 @@
{{define "title"}}Home{{end}}
{{define "main"}}
<h1>Welcome</h1>
<h1>Welcome {{.}}</h1>
<button hx-post="/api/logout" class="btn">Logout</button>
{{end}}