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,13 +11,44 @@ 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 {
type indexContent struct {
AuthedData authedData
LoginSectionData LoginSectionData
}
type authedData struct {
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
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, 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
}
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{
@ -27,31 +58,15 @@ func renderIndexPage(session sessions.SessionData, w http.ResponseWriter) {
Header: headerData{
Title: session.RoomId,
},
Content: MainData{
fmt.Sprintf("%+v", session),
"hello!",
session,
Content: indexContent{
AuthedData: authData,
},
}
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.ExecuteTemplate(w, "full-page", data)
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)
}
}
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")
// TODO return error i guess
}
renderIndexPage(session, w)
})
}

View File

@ -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

View File

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

View File

@ -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));

View File

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

View File

@ -1,42 +1,33 @@
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Untitled</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script
src="https://unpkg.com/htmx.org@1.9.6"
integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni"
crossorigin="anonymous"
></script>
<link
rel="stylesheet"
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]-->
<body>
{{ define "main-content" }}
<main>
<main
class="grid grid-cols-2 h-full"
>
{{ if not .AuthedData.IsZero }}
<section>
<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 }}"
<p>Your session is {{ .AuthedData.SessionStringToken }}</p>
<p>Some string is {{ .AuthedData.SomeString }}</p>
<a
href="/room/{{ .AuthedData.Session.RoomId }}"
class="text-blue-700 underline"
>You've logged into a room {{ .SessionToken.RoomId }}</a>
>You've logged into a room {{ .AuthedData.Session.RoomId }}</a
>
</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>

View File

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

View File

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