feat: login form or room info on index page
This commit is contained in:
parent
0e804b21e5
commit
c8f28bf0de
|
@ -11,13 +11,44 @@ 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 {
|
}
|
||||||
|
type authedData struct {
|
||||||
SessionStringToken string
|
SessionStringToken string
|
||||||
SomeString 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{
|
data := pageData{
|
||||||
|
@ -27,31 +58,15 @@ func renderIndexPage(session sessions.SessionData, w http.ResponseWriter) {
|
||||||
Header: headerData{
|
Header: headerData{
|
||||||
Title: session.RoomId,
|
Title: session.RoomId,
|
||||||
},
|
},
|
||||||
Content: MainData{
|
Content: indexContent{
|
||||||
fmt.Sprintf("%+v", session),
|
AuthedData: authData,
|
||||||
"hello!",
|
|
||||||
session,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
|
tmpl := template.Must(template.ParseFS(templateFs, pageTemplFile, baseFile, loginSectionFile))
|
||||||
err := tmpl.ExecuteTemplate(w, "full-page", data)
|
err = tmpl.ExecuteTemplate(w, "full-page", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("my error in executing template, huh\n %s", err)
|
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)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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));
|
||||||
|
|
|
@ -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}}
|
||||||
|
|
|
@ -1,42 +1,33 @@
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html class="no-js" lang="">
|
<html class="no-js" lang="">
|
||||||
<head>
|
<body>
|
||||||
<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]-->
|
|
||||||
{{ define "main-content" }}
|
{{ define "main-content" }}
|
||||||
<main>
|
<main
|
||||||
|
class="grid grid-cols-2 h-full"
|
||||||
|
>
|
||||||
|
{{ if not .AuthedData.IsZero }}
|
||||||
|
<section>
|
||||||
<h1>Hello</h1>
|
<h1>Hello</h1>
|
||||||
<p>This is index</p>
|
<p>This is index</p>
|
||||||
<p>Your session is {{ .SessionStringToken }}</p>
|
<p>Your session is {{ .AuthedData.SessionStringToken }}</p>
|
||||||
<p>Some string is {{ .SomeString }}</p>
|
<p>Some string is {{ .AuthedData.SomeString }}</p>
|
||||||
<a href="/room/{{ .SessionToken.RoomId }}"
|
<a
|
||||||
|
href="/room/{{ .AuthedData.Session.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
|
||||||
|
>
|
||||||
|
</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>
|
</main>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -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 }}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue