package routes import ( "embed" "fmt" "html/template" "log" "net/http" "sunshine.industries/some-automoderation/rooms" "sunshine.industries/some-automoderation/sessions" ) func registerLoginRoutes( templateFs *embed.FS, sessionSM sessions.SessionManagement, roomsM rooms.RoomManager, ) { // login page http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { var templFile = "templates/login.gohtml" tmpl := template.Must(template.ParseFS(templateFs, templFile)) err := tmpl.Execute(w, nil) if err != nil { log.Printf("my error in executing template, huh\n %s", err) } }) // 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, int64(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) { err := r.ParseForm() if err != nil { // TODO return error notice somehow w.WriteHeader(http.StatusBadRequest) } roomName := r.PostFormValue("roomName") _, exists, _ := roomsM.Get(roomName) if exists { // TODO return anouther error notice log.Printf("error, room name occupied", roomName) return } newRoom := rooms.Room{ Name: roomName, PasswordHash: r.PostFormValue("roomPassword"), // TODO hash the password, not to store AdminIds: []int64{1}, Paricipants: []rooms.Person{ { PersonId: 1, Name: r.PostFormValue("personalName"), PasswordHash: r.PostFormValue("personalPassword"), }, }, } err = roomsM.Save(newRoom) http.SetCookie(w, &http.Cookie{ Name: "auth", Value: "session-id", Secure: true, HttpOnly: true, Path: "/", }) if err != nil { log.Printf("what am i to do? error saving room %s", err) // todo return error notice somehow } var templFile = "templates/index.gohtml" tmpl := template.Must(template.ParseFS(templateFs, templFile)) w.Header().Add("HX-Retarget", "body") w.Header().Add("HX-Push-Url", "/") err = tmpl.Execute(w, nil) 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) { err := r.ParseForm() if err != nil { w.WriteHeader(http.StatusBadRequest) } roomName := r.PostFormValue("roomName") _, isFound, err := roomsM.Get(roomName) if err != nil { log.Printf("/login/room-name-check error finding room %s\n", err) } var templFile = "templates/login.gohtml" tmpl := template.Must(template.ParseFS(templateFs, templFile)) err = tmpl.ExecuteTemplate(w, "formButton", isFound) }) }