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) }