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]>
40 lines
943 B
Go
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)
|
|
}
|