Beignet API reference
    Preparing search index...

    Function createAuthHooks

    • Create route-scoped authentication hooks.

      The outer call binds the app context; the inner call takes auth options and infers the added context from resolve:

      const auth = createAuthHooks<AppContext>()({
      resolve: ({ ctx }) => (ctx.auth ? { user: ctx.auth.user } : null),
      });

      Use auth.required() on routes that require an authenticated actor and auth.optional() where handlers can use auth when present. The returned route hooks enrich handler ctx; business authorization still belongs in feature policies or use cases.

      Declare a headers schema when credentials live in request headers. The hook validates the raw lowercase header record itself, so resolve receives typed headers without contract casts and a required() hook rejects missing or malformed credentials with a framework-owned 401.

      Type Parameters

      • Ctx

      Returns <
          AddedCtx extends object & { gate?: undefined },
          HeadersSchema extends StandardSchema | undefined = undefined,
      >(
          options: AuthHooksOptions<Ctx, AddedCtx, HeadersSchema>,
      ) => AuthRouteHooks<Ctx, AddedCtx>

      A function that takes auth options and returns public, optional, and required route-hook factories.