template-monorepo/internal/logic/member/actor.go

29 lines
764 B
Go
Raw Normal View History

2026-05-20 23:51:22 +00:00
package member
import (
"context"
"fmt"
)
type actorKey struct{}
// Actor identifies the calling member (JWT middleware or dev headers).
2026-05-20 23:51:22 +00:00
type Actor struct {
TenantID string
UID string
}
// WithActor stores tenant/uid on the context for member logic handlers.
func WithActor(ctx context.Context, tenantID, uid string) context.Context {
return context.WithValue(ctx, actorKey{}, Actor{TenantID: tenantID, UID: uid})
}
// ActorFromContext reads the member actor injected by JWT middleware or dev headers.
2026-05-20 23:51:22 +00:00
func ActorFromContext(ctx context.Context) (Actor, error) {
v, ok := ctx.Value(actorKey{}).(Actor)
if !ok || v.TenantID == "" || v.UID == "" {
return Actor{}, fmt.Errorf("missing bearer token or X-Tenant-ID/X-UID headers")
2026-05-20 23:51:22 +00:00
}
return v, nil
}