Created
July 18, 2020 03:35
-
-
Save kendellfab/d497e588af79083ae2231aa2d48d4cbe 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
import ( | |
"context" | |
"net/http" | |
) | |
type key int | |
var contextKey = key(22) | |
func SetUser(usr *User, r *http.Request) { | |
ctx := r.Context() | |
ctx = context.WithValue(ctx, contextKey, usr) | |
*r = *(r.WithContext(ctx)) | |
} | |
func FromContext(r *http.Request) (*User, bool) { | |
usr, ok := r.Context().Value(contextKey).(*User) | |
return usr, ok | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment