github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/auth/context.go (about)

     1  package auth
     2  
     3  import "context"
     4  
     5  type userKey struct{}
     6  
     7  func ContextWithUser[T any](ctx context.Context, user T) context.Context {
     8  	return context.WithValue(ctx, (*userKey)(nil), user)
     9  }
    10  
    11  func UserFromContext[T any](ctx context.Context) (T, bool) {
    12  	user, loaded := ctx.Value((*userKey)(nil)).(T)
    13  	return user, loaded
    14  }