feat: create room endpoint
not really sets the cookie, but returns the rendered index on success
This commit is contained in:
parent
201b3760da
commit
850b6c693b
|
@ -68,6 +68,6 @@ func (redisRM RedisRM) Get(roomName string) (Room, bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (redisRM RedisRM) Save(room Room) error {
|
func (redisRM RedisRM) Save(room Room) error {
|
||||||
err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), room, 0).Err() // maybe even set expiration?
|
err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), &room, 0).Err() // maybe even set expiration?
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
func registerLoginRoutes(
|
func registerLoginRoutes(
|
||||||
templateFs *embed.FS,
|
templateFs *embed.FS,
|
||||||
sessionSM sessions.SessionManagement,
|
sessionSM sessions.SessionManagement,
|
||||||
rooms rooms.RoomManager,
|
roomsM rooms.RoomManager,
|
||||||
) {
|
) {
|
||||||
// login page
|
// login page
|
||||||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -38,7 +38,7 @@ func registerLoginRoutes(
|
||||||
pn := r.PostFormValue("personalName")
|
pn := r.PostFormValue("personalName")
|
||||||
pp := r.PostFormValue("personalPassword")
|
pp := r.PostFormValue("personalPassword")
|
||||||
|
|
||||||
room, _, err := rooms.Get(rn)
|
room, _, err := roomsM.Get(rn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("/login/submit error getting room %s", rn)
|
log.Printf("/login/submit error getting room %s", rn)
|
||||||
// return i guess
|
// return i guess
|
||||||
|
@ -60,16 +60,56 @@ func registerLoginRoutes(
|
||||||
// b) get room data
|
// b) get room data
|
||||||
// c) check if such person exists,
|
// c) check if such person exists,
|
||||||
// either create one, or check password
|
// either create one, or check password
|
||||||
// d) how should i monitor sessions?
|
// d) how should i monitor sessions? - space in redis
|
||||||
|
// so save session to redis and add cookie with sessionId
|
||||||
})
|
})
|
||||||
|
|
||||||
// when room does not exist
|
// when room does not exist
|
||||||
http.HandleFunc("/login/create", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/login/create", func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// TODO return error notice somehow
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "got submitted form %+v", r.PostForm)
|
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
|
// checking whether the room name already exists
|
||||||
|
@ -80,7 +120,7 @@ func registerLoginRoutes(
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
roomName := r.PostFormValue("roomName")
|
roomName := r.PostFormValue("roomName")
|
||||||
_, isFound, err := rooms.Get(roomName)
|
_, isFound, err := roomsM.Get(roomName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("/login/room-name-check error finding room %s\n", err)
|
log.Printf("/login/room-name-check error finding room %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue