http session context helper function.

This commit is contained in:
Miroslav Šedivý 2020-11-14 18:22:15 +01:00
parent 40b986c8be
commit 7524f20cb7

View File

@ -47,7 +47,7 @@ func (api *ApiManagerCtx) Route(r chi.Router) {
r.Route("/room", roomHandler.Route) r.Route("/room", roomHandler.Route)
r.Get("/test", func(w http.ResponseWriter, r *http.Request) { r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
session, _ := r.Context().Value(keySessionCtx).(types.Session) session := GetSession(r)
utils.HttpBadRequest(w, "Hi `" + session.ID() + "`, you are authenticated.") utils.HttpBadRequest(w, "Hi `" + session.ID() + "`, you are authenticated.")
}) })
} }
@ -63,3 +63,7 @@ func (api *ApiManagerCtx) Authenticate(next http.Handler) http.Handler {
} }
}) })
} }
func GetSession(r *http.Request) types.Session {
return r.Context().Value(keySessionCtx).(types.Session)
}