diff --git a/pkg/viewer/viewr.go b/pkg/viewer/viewr.go new file mode 100644 index 00000000..46424bb2 --- /dev/null +++ b/pkg/viewer/viewr.go @@ -0,0 +1,27 @@ +// Package viewer provides utility functions for passing the current user to a context. +package viewer + +import "context" + +type key int + +const viewerKey key = 0 + +// Viewer represents a user performing an action on the server. +type Viewer struct { + UserID string + Username string + FullName string + SessionID string +} + +// FromContext returns the current user information if present. +func FromContext(ctx context.Context) (Viewer, bool) { + v, ok := ctx.Value(viewerKey).(Viewer) + return v, ok +} + +// NewContext updates the parent context with viewer information. +func NewContext(ctx context.Context, v Viewer) context.Context { + return context.WithValue(ctx, viewerKey, v) +}