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)
}
}

View File

@ -20,7 +20,7 @@ type SessionData struct {
type SessionManagement interface {
Get(sessionId int) SessionData
Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error)
Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error)
Remove(ctx context.Context, sessionId int) error
}
@ -45,7 +45,7 @@ func (redisSM RedisSM) Get(sessionId int) SessionData {
log.Printf("> successfully found %d %+v", sessionId, foundSession)
return foundSession
}
func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) {
func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error) {
randId := rand.Int()
newSession := SessionData{
SessionId: randId,
@ -57,9 +57,9 @@ func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms
if err != nil {
log.Printf("> error! saving session %+v %s", newSession, err)
return 0, fmt.Errorf("error saving new session: %+v with %s", newSession, err)
return SessionData{}, fmt.Errorf("error saving new session: %+v with %s", newSession, err)
}
return randId, nil
return newSession, nil
}
func (redisSM RedisSM) Remove(ctx context.Context, sessionId int) error {
@ -73,9 +73,9 @@ func (d DummySM) Get(sessionId int) SessionData {
log.Printf("get dummy session by %d", sessionId)
return SessionData{}
}
func (d DummySM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) {
func (d DummySM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error) {
log.Printf("save dummy session with %s %d", roomName, personId)
return 1, nil
return SessionData{}, nil
}
func (d DummySM) Remove(ctx context.Context, sessionId int) error {
log.Printf("deleting session %d", sessionId)