Skip to content

Instantly share code, notes, and snippets.

@AlecAivazis
Created May 31, 2019 01:50
Show Gist options
  • Save AlecAivazis/0c871de19f4b9c98a66c46dd111309ef to your computer and use it in GitHub Desktop.
Save AlecAivazis/0c871de19f4b9c98a66c46dd111309ef to your computer and use it in GitHub Desktop.
func withUserInfo(handler http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// look up the value of the Authorization header
tokenValue := r.Header.Get("Authorization")
// here is where you would perform some kind of validation on the token
// but we're going to skip that for this example and save it as the
// id directly. PLEASE, DO NOT DO THIS IN PRODUCTION.
// invoke the handler with the new context
handler.ServeHTTP(w, r.WithContext(
context.WithValue(r.Context(), "user-id", tokenValue),
))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment