From c8f28bf0de7a19d3dafe5dd1cf311ff3433481f3 Mon Sep 17 00:00:00 2001 From: efim Date: Sat, 18 Nov 2023 12:18:56 +0000 Subject: [PATCH] feat: login form or room info on index page --- routes/index_page.go | 79 ++++++++++++++++----------- routes/login_page.go | 35 ++++++++---- routes/routes.go | 4 +- routes/static/out.css | 15 +++-- routes/templates/base.gohtml | 2 +- routes/templates/index.gohtml | 67 ++++++++++------------- routes/templates/login-section.gohtml | 2 - routes/templates/login.gohtml | 2 + 8 files changed, 113 insertions(+), 93 deletions(-) diff --git a/routes/index_page.go b/routes/index_page.go index f94189b..537c805 100644 --- a/routes/index_page.go +++ b/routes/index_page.go @@ -11,47 +11,62 @@ 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) - } - +type indexContent struct { + AuthedData authedData + LoginSectionData LoginSectionData +} +type authedData struct { + SessionStringToken string + SomeString string + Session sessions.SessionData } +func (ad authedData) IsZero() bool { + return ad == authedData{} +} + +// index page shows info on logged in room OR login room form on the left +// and general automoderation rules on the right func indexPageRoute( templateFs *embed.FS, sessionSM sessions.SessionManagement, roomsM rooms.RoomManager, ) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - session, found := getContextSession(r.Context()) - if !found { - log.Printf("/ session not found, should be impossible") + session, err := getRequestSession(r, sessionSM) + if err != nil { + log.Printf("/ session not found, means should render the login section %s", err) // TODO return error i guess } - renderIndexPage(session, w) + baseFile := "templates/base.gohtml" + pageTemplFile := "templates/index.gohtml" + loginSectionFile := "templates/login-section.gohtml" + + authData := authedData{} + if err == nil { + authData = authedData{ + fmt.Sprintf("%+v", session), + "hello!", + session, + } + } + + data := pageData{ + Base: baseData{ + Title: "hello base template title", + }, + Header: headerData{ + Title: session.RoomId, + }, + Content: indexContent{ + AuthedData: authData, + }, + } + tmpl := template.Must(template.ParseFS(templateFs, pageTemplFile, baseFile, loginSectionFile)) + err = tmpl.ExecuteTemplate(w, "full-page", data) + if err != nil { + log.Printf("my error in executing template, huh\n %s", err) + } + }) } diff --git a/routes/login_page.go b/routes/login_page.go index 74fc632..d5d8773 100644 --- a/routes/login_page.go +++ b/routes/login_page.go @@ -3,6 +3,7 @@ package routes import ( "context" "embed" + "errors" "fmt" "html/template" "log" @@ -55,6 +56,27 @@ func getContextSession(ctx context.Context) (sessions.SessionData, bool) { } } +var ErrAuthCookieMissing = errors.New("auth cookie is missing") +var ErrAuthCookieValueInvalid = errors.New("auth cookie value is not decoeable") +var ErrAuthSessionNotFound = errors.New("session not found") + +func getRequestSession(r *http.Request, + sessionsM sessions.SessionManagement) (sessions.SessionData, error) { + sessionCookie, err := r.Cookie(authCookieName) + if err != nil { + return sessions.SessionData{}, ErrAuthCookieMissing + } + sessionId, err := strconv.Atoi(sessionCookie.Value) + if err != nil { + return sessions.SessionData{}, ErrAuthCookieValueInvalid + } + session := sessionsM.Get(sessionId) + if session == (sessions.SessionData{}) { + return sessions.SessionData{}, ErrAuthSessionNotFound + } + return session, nil +} + // checks sessionId from cookie // when non-zero session found - pass to next http.Hander // when no session available - render same as login page and redirect to / @@ -72,21 +94,10 @@ func authedPageMiddleware( next.ServeHTTP(w, r.WithContext(ctx)) } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - sessionCookie, err := r.Cookie(authCookieName) + session, err := getRequestSession(r, sessionsM) if err != nil { returnNoAccess(w, r) return - } - // TODO log here, why i get 'error readin 0' - sessionId, err := strconv.Atoi(sessionCookie.Value) - if err != nil { - returnNoAccess(w, r) - return - } - session := sessionsM.Get(sessionId) - if session == (sessions.SessionData{}) { - returnNoAccess(w, r) - return } else { rerturnSuccess(w, r, session) return diff --git a/routes/routes.go b/routes/routes.go index c2a3da7..9bcfdc2 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -19,9 +19,7 @@ func RegisterRoutes(sessionsM sessions.SessionManagement, rooms rooms.RoomManage registerLoginRoutes(&templateFs, sessionsM, rooms) // main page template - http.Handle("/", authedPageMiddleware( - sessionsM, - indexPageRoute(&templateFs, sessionsM, rooms))) + http.Handle("/", indexPageRoute(&templateFs, sessionsM, rooms)) // main conversation room page registerPageRoutes(&templateFs, sessionsM, rooms) diff --git a/routes/static/out.css b/routes/static/out.css index f7b36b5..8057955 100644 --- a/routes/static/out.css +++ b/routes/static/out.css @@ -558,6 +558,11 @@ video { height: 2rem; } +.h-fit { + height: -moz-fit-content; + height: fit-content; +} + .h-full { height: 100%; } @@ -570,6 +575,11 @@ video { width: 5rem; } +.w-fit { + width: -moz-fit-content; + width: fit-content; +} + .w-full { width: 100%; } @@ -676,11 +686,6 @@ video { background-color: hsl(var(--brick-color) / 0.5); } -.bg-amber-400 { - --tw-bg-opacity: 1; - background-color: rgb(251 191 36 / var(--tw-bg-opacity)); -} - .bg-blue-200 { --tw-bg-opacity: 1; background-color: rgb(191 219 254 / var(--tw-bg-opacity)); diff --git a/routes/templates/base.gohtml b/routes/templates/base.gohtml index 17e5093..934f170 100644 --- a/routes/templates/base.gohtml +++ b/routes/templates/base.gohtml @@ -23,7 +23,7 @@