Created
May 31, 2019 01:50
-
-
Save AlecAivazis/0c871de19f4b9c98a66c46dd111309ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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