Files
stpweb/backend/graph/directives.go
T
adamfandClaude Opus 5.5 4530cb8dc4
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s
Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo,
keep short ones for security boundaries, upstream quirks and ordering
constraints, and record the convention in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 20:49:03 +00:00

40 lines
943 B
Go

package graph
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
)
// The @admin and @signedIn directives. The front end shows these error
// strings verbatim (the tracker reads "admin access required" as a lapsed
// session), so don't reword them.
const (
errAdminRequired = "admin access required"
errSignedInRequired = "authentication required"
)
// Directives is shared by main and the tests. A nil directive fails closed.
func Directives() DirectiveRoot {
return DirectiveRoot{
Admin: requireAdmin,
SignedIn: requireSignedIn,
}
}
func requireAdmin(ctx context.Context, _ any, next graphql.Resolver) (any, error) {
if !IsAdminFromCtx(ctx) {
return nil, fmt.Errorf(errAdminRequired)
}
return next(ctx)
}
func requireSignedIn(ctx context.Context, _ any, next graphql.Resolver) (any, error) {
if _, ok := UserIDFromCtx(ctx); !ok {
return nil, fmt.Errorf(errSignedInRequired)
}
return next(ctx)
}