context.go 480 B

12345678910111213141516171819
  1. package logging
  2. import (
  3. "context"
  4. )
  5. type userIDKey struct{}
  6. // WithUserID decodes the token, extracts the user from the payload and stores
  7. // it in the context.
  8. func WithUserID(ctx context.Context, userID string) context.Context {
  9. return context.WithValue(ctx, userIDKey{}, userID)
  10. }
  11. // GetUserID returns the request id from the context if it exists.
  12. func GetUserID(ctx context.Context) (string, bool) {
  13. v, ok := ctx.Value(userIDKey{}).(string)
  14. return v, ok && v != ""
  15. }