feat: create|login on room path

This commit is contained in:
efim 2023-11-18 13:41:54 +00:00
parent c8f28bf0de
commit d87c102d95
4 changed files with 32 additions and 23 deletions

View File

@ -20,6 +20,7 @@ import (
type LoginSectionData struct { type LoginSectionData struct {
IsRoomExisting bool IsRoomExisting bool
RoomName string
} }
type loginPageData struct { type loginPageData struct {
LoginSection LoginSectionData LoginSection LoginSectionData
@ -33,7 +34,7 @@ func registerLoginRoutes(
) { ) {
// login page // login page
http.HandleFunc(loginPath, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(loginPath, func(w http.ResponseWriter, r *http.Request) {
renderLoginPage(w) renderLoginPage(w, "")
}) })
http.HandleFunc("/login/join", joinRoomHandler(templateFs, sessionSM, roomsM)) http.HandleFunc("/login/join", joinRoomHandler(templateFs, sessionSM, roomsM))
http.HandleFunc("/login/create", createRoomHandler(templateFs, sessionSM, roomsM)) http.HandleFunc("/login/create", createRoomHandler(templateFs, sessionSM, roomsM))
@ -86,7 +87,7 @@ func authedPageMiddleware(
returnNoAccess := func(w http.ResponseWriter, r *http.Request) { returnNoAccess := func(w http.ResponseWriter, r *http.Request) {
log.Printf("auth middle > restricting access to %s", r.URL.Path) log.Printf("auth middle > restricting access to %s", r.URL.Path)
w.Header().Add("HX-Replace-Url", loginPath) w.Header().Add("HX-Replace-Url", loginPath)
renderLoginPage(w) renderLoginPage(w, "")
} }
rerturnSuccess := func(w http.ResponseWriter, r *http.Request, session sessions.SessionData) { rerturnSuccess := func(w http.ResponseWriter, r *http.Request, session sessions.SessionData) {
ctx := context.WithValue(r.Context(), contextKey("session"), session) ctx := context.WithValue(r.Context(), contextKey("session"), session)
@ -105,18 +106,24 @@ func authedPageMiddleware(
}) })
} }
func renderLoginPage(w http.ResponseWriter) { func renderLoginPage(w http.ResponseWriter, roomName string) {
baseFile := "templates/base.gohtml" baseFile := "templates/base.gohtml"
pageTempl := "templates/login.gohtml" pageTempl := "templates/login.gohtml"
loginSecion := "templates/login-section.gohtml" loginSecion := "templates/login-section.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, pageTempl, baseFile, loginSecion)) tmpl := template.Must(template.ParseFS(templateFs, pageTempl, baseFile, loginSecion))
title := "Some Automoderation: Join room or create one"
if roomName != "" {
title = fmt.Sprintf("Some Automoderation: create or join '%s' room", roomName)
}
data := pageData{ data := pageData{
Base: baseData{ Base: baseData{
Title: "login", Title: title,
}, },
Content: loginPageData{ Content: loginPageData{
LoginSection: LoginSectionData{ LoginSection: LoginSectionData{
IsRoomExisting: false, IsRoomExisting: false,
RoomName: roomName,
}, },
}, },
} }

View File

@ -25,9 +25,7 @@ func registerPageRoutes(
roomsM rooms.RoomManager, roomsM rooms.RoomManager,
) { ) {
http.Handle(roomPath, // ending in / captures all following path sections, i.e room name http.Handle(roomPath, // ending in / captures all following path sections, i.e room name
authedPageMiddleware( http.StripPrefix(roomPath, roomPageRoute(templateFs, roomsM, sessionSM)))
sessionSM,
http.StripPrefix(roomPath, roomPageRoute(templateFs, roomsM))))
http.Handle(raiseHandPath, // ending in / captures all following path sections, i.e gesture num http.Handle(raiseHandPath, // ending in / captures all following path sections, i.e gesture num
authedPageMiddleware( authedPageMiddleware(
@ -165,6 +163,7 @@ func releaseHandRoute(
func roomPageRoute( func roomPageRoute(
templateFs *embed.FS, templateFs *embed.FS,
roomsM rooms.RoomManager, roomsM rooms.RoomManager,
sessionSM sessions.SessionManagement,
) http.HandlerFunc { ) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
roomName := r.URL.Path roomName := r.URL.Path
@ -176,24 +175,16 @@ func roomPageRoute(
} }
// check session, // check session,
session, found := getContextSession(r.Context()) session, err := getRequestSession(r, sessionSM)
if !found { if err != nil || session.RoomId != roomName {
log.Printf("session not found %t", found) log.Printf("not authed with session %+v | error %s, but for wrong room, trying to access %s", session, err, roomName)
// TODO here will be rendering of renderLoginPage(w, roomName)
// 'create this room' or 'join this room'
// but yeah, let's just use auth middle that redirects to /
w.Header().Add("HX-Redirect", "/")
return
}
if session.RoomId != roomName {
log.Printf("session found %+v, but for wrong room, trying to access %s", session, roomName)
w.Header().Add("HX-Redirect", "/")
return return
} }
room, found, err := roomsM.Get(roomName) room, found, err := roomsM.Get(roomName)
if err != nil || !found { if err != nil || !found {
log.Printf("/room room for name %s not found or err: %s / found %s", roomName, err, found) log.Printf("/room room for name %s not found or err: %s / found %t", roomName, err, found)
// TODO here should be append to error place // TODO here should be append to error place
w.Header().Add("HX-Redirect", "/") w.Header().Add("HX-Redirect", "/")
return return

View File

@ -773,3 +773,11 @@ video {
--tw-bg-opacity: 1; --tw-bg-opacity: 1;
background-color: rgb(185 28 28 / var(--tw-bg-opacity)); background-color: rgb(185 28 28 / var(--tw-bg-opacity));
} }
.read-only\:font-bold:-moz-read-only {
font-weight: 700;
}
.read-only\:font-bold:read-only {
font-weight: 700;
}

View File

@ -9,13 +9,16 @@
id="roomName" id="roomName"
type="text" type="text"
name="roomName" name="roomName"
value="" value="{{.RoomName}}"
placeholder="room name!!!" placeholder="room name"
hx-trigger="keyup changed delay:500ms" hx-trigger="keyup changed delay:500ms"
hx-post="/login/room-name-check" hx-post="/login/room-name-check"
hx-target="#formButton" hx-target="#formButton"
hx-swap="outerHTML" hx-swap="outerHTML"
class="invalid:bg-red-700" class="invalid:bg-red-700 read-only:font-bold"
{{ if not (eq .RoomName "") }}
readonly
{{ end }}
/> />
<input <input
id="roomPassword" id="roomPassword"