get player id with middleware, not session

This commit is contained in:
Branden J Brown 2024-02-02 13:03:05 -06:00
parent 438c70b41b
commit ca4823e80b
2 changed files with 7 additions and 18 deletions

View File

@ -61,7 +61,7 @@ func WithSession(sessions player.RowQuerier) func(http.Handler) http.Handler {
} }
} }
// Session returns the session ID set by WithSession in the request context. // ReqPlayer returns the session ID set by WithSession in the request context.
func Session(ctx context.Context) player.Session { func ReqPlayer(ctx context.Context) player.ID {
return value[player.Session](ctx) return value[player.ID](ctx)
} }

View File

@ -135,31 +135,20 @@ func (s *Server) Login(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) Me(w http.ResponseWriter, r *http.Request) { func (s *Server) Me(w http.ResponseWriter, r *http.Request) {
id := serve.Session(r.Context()) id := serve.ReqPlayer(r.Context())
if id == (player.Session{}) { if id == (player.ID{}) {
panic("missing player ID") panic("missing player ID")
} }
p, err := player.FromSession(r.Context(), s.sessions, id, time.Now())
if err != nil {
panic("session with no player id: " + err.Error())
}
q := struct { q := struct {
ID player.ID `json:"id"` ID player.ID `json:"id"`
}{p} }{id}
json.NewEncoder(w).Encode(q) json.NewEncoder(w).Encode(q)
} }
// Queue connects players to games. The connection immediately upgrades. // Queue connects players to games. The connection immediately upgrades.
// This handler MUST be wrapped in [serve.WithPlayerID]. // This handler MUST be wrapped in [serve.WithPlayerID].
func (s *Server) Queue(w http.ResponseWriter, r *http.Request) { func (s *Server) Queue(w http.ResponseWriter, r *http.Request) {
id := serve.Session(r.Context()) p := serve.ReqPlayer(r.Context())
if id == (player.Session{}) {
panic("missing player ID")
}
p, err := player.FromSession(r.Context(), s.sessions, id, time.Now())
if p == (player.ID{}) {
panic("session with no player id: " + err.Error())
}
person, err := s.accept(w, r, p) person, err := s.accept(w, r, p)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)