diff --git a/routes/login_page.go b/routes/login_page.go index 2b56150..0b632a3 100644 --- a/routes/login_page.go +++ b/routes/login_page.go @@ -14,6 +14,21 @@ import ( "sunshine.industries/some-automoderation/sessions" ) +// function to register all http routes for servicing auth pages and logic +func registerLoginRoutes( + templateFs *embed.FS, + sessionSM sessions.SessionManagement, + roomsM rooms.RoomManager, +) { + // login page + http.HandleFunc(loginPath, func(w http.ResponseWriter, r *http.Request) { + renderLoginPage(w) + }) + http.HandleFunc("/login/join", joinRoomHandler(templateFs, sessionSM, roomsM)) + http.HandleFunc("/login/create", createRoomHandler(templateFs, sessionSM, roomsM)) + http.HandleFunc("/login/room-name-check", checkRoomName(templateFs, roomsM)) +} + const authCookieName = "auth" const loginPath = "/login" type contextKey string @@ -24,7 +39,6 @@ type contextKey string func authedPageMiddleware( sessionsM sessions.SessionManagement, next http.Handler, ) http.Handler { - returnNoAccess := func(w http.ResponseWriter, r *http.Request) { log.Printf("auth middle > restricting access to %s", r.URL.Path) w.Header().Add("HX-Replace-Url", loginPath) @@ -35,7 +49,6 @@ func authedPageMiddleware( log.Printf("auth middle > allowing access to %s for %+v", r.URL.Path, session) next.ServeHTTP(w, r.WithContext(ctx)) } - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { sessionCookie, err := r.Cookie(authCookieName) if err != nil { @@ -68,57 +81,11 @@ func renderLoginPage(w http.ResponseWriter) { } } -// function to register all http routes for servicing auth pages and logic -func registerLoginRoutes( - templateFs *embed.FS, +func createRoomHandler( templateFs *embed.FS, sessionSM sessions.SessionManagement, roomsM rooms.RoomManager, -) { - // login page - http.HandleFunc(loginPath, func(w http.ResponseWriter, r *http.Request) { - renderLoginPage(w) - }) - - // when the room exists - attempt to join - http.HandleFunc("/login/join", func(w http.ResponseWriter, r *http.Request) { - err := r.ParseForm() - if err != nil { - w.WriteHeader(http.StatusBadRequest) - } - - rn := r.PostFormValue("roomName") - rp := r.PostFormValue("roomPassword") - pn := r.PostFormValue("personalName") - pp := r.PostFormValue("personalPassword") - - room, _, err := roomsM.Get(rn) - if err != nil { - log.Printf("/login/submit error getting room %s", rn) - // return i guess - } else { - log.Printf("/login/submit found room %+v", room) - } - - roomId := "room-name-actually" // would be taken from rooms interface from redis - // would be either taken from room info on correct person pass or created - personId := 111 - id, err := sessionSM.Save(roomId, personId) - if err != nil { - log.Printf("/login/submit > error saving session %s", err) - } - - fmt.Fprintf(w, "is is %d. room things %s & %s, personal things %s and %s. \n found room %+v", id, rn, rp, pn, pp, room) - // i suppose here i'll need to - // a) check if room password OK - // b) get room data - // c) check if such person exists, - // either create one, or check password - // d) how should i monitor sessions? - space in redis - // so save session to redis and add cookie with sessionId - }) - - // when room does not exist - http.HandleFunc("/login/create", func(w http.ResponseWriter, r *http.Request) { +) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { // TODO return error notice somehow @@ -167,11 +134,15 @@ func registerLoginRoutes( if err != nil { log.Printf("my error in executing template, huh\n %s", err) } - }) + } +} - // checking whether the room name already exists - // toggle button between Join or Create - http.HandleFunc("/login/room-name-check", func(w http.ResponseWriter, r *http.Request) { +// checking whether the room name already exists +// toggle button between Join or Create +func checkRoomName( templateFs *embed.FS, + roomsM rooms.RoomManager, +) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { w.WriteHeader(http.StatusBadRequest) @@ -184,6 +155,48 @@ func registerLoginRoutes( var templFile = "templates/login.gohtml" tmpl := template.Must(template.ParseFS(templateFs, templFile)) err = tmpl.ExecuteTemplate(w, "formButton", isFound) - }) - + } } + +func joinRoomHandler( templateFs *embed.FS, + sessionSM sessions.SessionManagement, + roomsM rooms.RoomManager, +) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + w.WriteHeader(http.StatusBadRequest) + } + + rn := r.PostFormValue("roomName") + rp := r.PostFormValue("roomPassword") + pn := r.PostFormValue("personalName") + pp := r.PostFormValue("personalPassword") + + room, _, err := roomsM.Get(rn) + if err != nil { + log.Printf("/login/submit error getting room %s", rn) + // return i guess + } else { + log.Printf("/login/submit found room %+v", room) + } + + roomId := "room-name-actually" // would be taken from rooms interface from redis + // would be either taken from room info on correct person pass or created + personId := 111 + id, err := sessionSM.Save(roomId, personId) + if err != nil { + log.Printf("/login/submit > error saving session %s", err) + } + + fmt.Fprintf(w, "is is %d. room things %s & %s, personal things %s and %s. \n found room %+v", id, rn, rp, pn, pp, room) + // i suppose here i'll need to + // a) check if room password OK + // b) get room data + // c) check if such person exists, + // either create one, or check password + // d) how should i monitor sessions? - space in redis + // so save session to redis and add cookie with sessionId + } +} +