fix: rendering index page after login

This commit is contained in:
efim
2023-11-18 03:47:58 +00:00
parent 942091c5d8
commit cdd31e186a
3 changed files with 40 additions and 40 deletions

View File

@@ -11,42 +11,47 @@ import (
"sunshine.industries/some-automoderation/sessions"
)
func renderIndexPage(session sessions.SessionData, w http.ResponseWriter) {
var baseFile = "templates/base.gohtml"
var templFile = "templates/index.gohtml"
type MainData struct {
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
}
data := pageData{
Base: baseData{
Title: "hello base template title",
},
Header: headerData{
Title: session.RoomId,
},
Content: MainData{
fmt.Sprintf("%+v", session),
"hello!",
session,
},
}
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
}
func indexPageRoute(
templateFs *embed.FS,
sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager,
) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var baseFile = "templates/base.gohtml"
var templFile = "templates/index.gohtml"
session, found := getContextSession(r.Context())
if !found {
log.Printf("/ session not found, should be impossible")
// TODO return error i guess
}
type MainData struct {
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
}
data := pageData{
Base: baseData{
Title: "hello base template title",
},
Header: headerData{
Title: session.RoomId,
},
Content: MainData{
fmt.Sprintf("%+v", session),
"hello!",
session,
},
}
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
renderIndexPage(session, w)
})
}

View File

@@ -143,26 +143,21 @@ func createRoomHandler(templateFs *embed.FS,
log.Printf("what am i to do? error saving room %s", err)
// todo return error notice somehow
}
newSessionId, err := sessionSM.Save(r.Context(), newRoom.Name, person.Id)
newSession, err := sessionSM.Save(r.Context(), newRoom.Name, person.Id)
if err != nil {
log.Printf("what am i to do? error saving session %s", err)
// todo return error notice somehow
}
http.SetCookie(w, &http.Cookie{
Name: authCookieName,
Value: fmt.Sprint(newSessionId),
Value: fmt.Sprint(newSession.SessionId),
Secure: true,
HttpOnly: true,
Path: "/",
})
var templFile = "templates/index.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile))
w.Header().Add("HX-Retarget", "body")
w.Header().Add("HX-Push-Url", "/")
err = tmpl.Execute(w, nil)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
renderIndexPage(newSession, w)
}
}