feat: Pass user to index template
This commit is contained in:
parent
d2f2de8c50
commit
57ac5de1df
3 changed files with 9 additions and 5 deletions
|
|
@ -49,7 +49,6 @@ func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
session, bump, err := h.auth.ValidateSessionToken(r.Context(), cookie.Value)
|
session, bump, err := h.auth.ValidateSessionToken(r.Context(), cookie.Value)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -63,6 +62,13 @@ func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.SetCookie(w, auth.NewSessionCookie(cookie.Value, false))
|
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{
|
files := []string{
|
||||||
"./ui/html/base.tmpl",
|
"./ui/html/base.tmpl",
|
||||||
"./ui/html/pages/index.tmpl",
|
"./ui/html/pages/index.tmpl",
|
||||||
|
|
@ -75,7 +81,7 @@ func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ts.ExecuteTemplate(w, "base", nil)
|
err = ts.ExecuteTemplate(w, "base", user.Username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,6 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>{{template "main" .}}</main>
|
<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>
|
<script src="/static/js/main.js" type="text/javascript"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{{define "title"}}Home{{end}}
|
{{define "title"}}Home{{end}}
|
||||||
|
|
||||||
{{define "main"}}
|
{{define "main"}}
|
||||||
<h1>Welcome</h1>
|
<h1>Welcome {{.}}</h1>
|
||||||
<button hx-post="/api/logout" class="btn">Logout</button>
|
<button hx-post="/api/logout" class="btn">Logout</button>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue