feat: login form or room info on index page

This commit is contained in:
efim 2023-11-18 12:18:56 +00:00
parent 0e804b21e5
commit c8f28bf0de
8 changed files with 113 additions and 93 deletions

View File

@ -11,47 +11,62 @@ import (
"sunshine.industries/some-automoderation/sessions" "sunshine.industries/some-automoderation/sessions"
) )
func renderIndexPage(session sessions.SessionData, w http.ResponseWriter) { type indexContent struct {
var baseFile = "templates/base.gohtml" AuthedData authedData
var templFile = "templates/index.gohtml" LoginSectionData LoginSectionData
type MainData struct { }
SessionStringToken string type authedData struct {
SomeString string SessionStringToken string
SessionToken sessions.SessionData SomeString string
} Session 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 (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( func indexPageRoute(
templateFs *embed.FS, templateFs *embed.FS,
sessionSM sessions.SessionManagement, sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager, roomsM rooms.RoomManager,
) http.HandlerFunc { ) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, found := getContextSession(r.Context()) session, err := getRequestSession(r, sessionSM)
if !found { if err != nil {
log.Printf("/ session not found, should be impossible") log.Printf("/ session not found, means should render the login section %s", err)
// TODO return error i guess // 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)
}
}) })
} }

View File

@ -3,6 +3,7 @@ package routes
import ( import (
"context" "context"
"embed" "embed"
"errors"
"fmt" "fmt"
"html/template" "html/template"
"log" "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 // checks sessionId from cookie
// when non-zero session found - pass to next http.Hander // when non-zero session found - pass to next http.Hander
// when no session available - render same as login page and redirect to / // when no session available - render same as login page and redirect to /
@ -72,21 +94,10 @@ func authedPageMiddleware(
next.ServeHTTP(w, r.WithContext(ctx)) next.ServeHTTP(w, r.WithContext(ctx))
} }
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sessionCookie, err := r.Cookie(authCookieName) session, err := getRequestSession(r, sessionsM)
if err != nil { if err != nil {
returnNoAccess(w, r) returnNoAccess(w, r)
return 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 { } else {
rerturnSuccess(w, r, session) rerturnSuccess(w, r, session)
return return

View File

@ -19,9 +19,7 @@ func RegisterRoutes(sessionsM sessions.SessionManagement, rooms rooms.RoomManage
registerLoginRoutes(&templateFs, sessionsM, rooms) registerLoginRoutes(&templateFs, sessionsM, rooms)
// main page template // main page template
http.Handle("/", authedPageMiddleware( http.Handle("/", indexPageRoute(&templateFs, sessionsM, rooms))
sessionsM,
indexPageRoute(&templateFs, sessionsM, rooms)))
// main conversation room page // main conversation room page
registerPageRoutes(&templateFs, sessionsM, rooms) registerPageRoutes(&templateFs, sessionsM, rooms)

View File

@ -558,6 +558,11 @@ video {
height: 2rem; height: 2rem;
} }
.h-fit {
height: -moz-fit-content;
height: fit-content;
}
.h-full { .h-full {
height: 100%; height: 100%;
} }
@ -570,6 +575,11 @@ video {
width: 5rem; width: 5rem;
} }
.w-fit {
width: -moz-fit-content;
width: fit-content;
}
.w-full { .w-full {
width: 100%; width: 100%;
} }
@ -676,11 +686,6 @@ video {
background-color: hsl(var(--brick-color) / 0.5); 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 { .bg-blue-200 {
--tw-bg-opacity: 1; --tw-bg-opacity: 1;
background-color: rgb(191 219 254 / var(--tw-bg-opacity)); background-color: rgb(191 219 254 / var(--tw-bg-opacity));

View File

@ -23,7 +23,7 @@
<!-- Place favicon.ico in the root directory --> <!-- Place favicon.ico in the root directory -->
</head> </head>
<body <body
class="flex flex-col" class="flex flex-col h-screen"
> >
<nav class="bg-yellow-200 flex-none h-14"> <nav class="bg-yellow-200 flex-none h-14">
{{if .Header.Title}} Room name: "{{.Header.Title}}"{{end}} {{if .Header.Title}} Room name: "{{.Header.Title}}"{{end}}

View File

@ -1,43 +1,34 @@
<!doctype html> <!doctype html>
<html class="no-js" lang=""> <html class="no-js" lang="">
<head> <body>
<meta charset="utf-8" /> {{ define "main-content" }}
<meta http-equiv="x-ua-compatible" content="ie=edge" /> <main
<title>Untitled</title> class="grid grid-cols-2 h-full"
<meta name="description" content="" /> >
<meta name="viewport" content="width=device-width, initial-scale=1" /> {{ if not .AuthedData.IsZero }}
<script <section>
src="https://unpkg.com/htmx.org@1.9.6" <h1>Hello</h1>
integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni" <p>This is index</p>
crossorigin="anonymous" <p>Your session is {{ .AuthedData.SessionStringToken }}</p>
></script> <p>Some string is {{ .AuthedData.SomeString }}</p>
<link <a
rel="stylesheet" href="/room/{{ .AuthedData.Session.RoomId }}"
href="/static/out.css"
type="text/css"
media="screen"
/>
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<!-- Place favicon.ico in the root directory -->
</head>
<body class="bg-amber-400 text-xl">
<!--[if lt IE 8]>
<p class="browserupgrade">
You are using an <strong>outdated</strong> browser. Please
<a href="http://browsehappy.com/">upgrade your browser</a> to improve
your experience.
</p>
<![endif]-->
{{ define "main-content" }}
<main>
<h1>Hello</h1>
<p>This is index</p>
<p>Your session is {{ .SessionStringToken }}</p>
<p>Some string is {{ .SomeString }}</p>
<a href="/room/{{ .SessionToken.RoomId }}"
class="text-blue-700 underline" class="text-blue-700 underline"
>You've logged into a room {{ .SessionToken.RoomId }}</a> >You've logged into a room {{ .AuthedData.Session.RoomId }}</a
</main> >
{{ end }} </section>
{{ else }}
<section class="border">
<h2>hoho, the session is missing. let's do the auth section</h2>
<div class="h-fit w-fit">
{{ template "loginSection" .LoginSectionData }}
</div>
</section>
{{ end }}
<section class="border">
<h2>Here be rules</h2>
</section>
</main>
{{ end }}
</body> </body>
</html> </html>

View File

@ -1,5 +1,4 @@
{{ define "loginSection" }} {{ define "loginSection" }}
<section class="h-full grid place-content-center">
<form <form
id="loginForm" id="loginForm"
class="grid grid-cols-2 place-content-center border border-black rounded p-4 gap-6" class="grid grid-cols-2 place-content-center border border-black rounded p-4 gap-6"
@ -107,5 +106,4 @@
this.setCustomValidity(""); this.setCustomValidity("");
}); });
</script> </script>
</section>
{{ end }} {{ end }}

View File

@ -33,7 +33,9 @@
<header> <header>
<h1>Some Automoderation: login page</h1> <h1>Some Automoderation: login page</h1>
</header> </header>
<section class="h-full grid place-content-center">
{{ template "loginSection" .LoginSection }} {{ template "loginSection" .LoginSection }}
</section>
</main> </main>
{{ end }} {{ end }}
</body> </body>