@darthcav/ts-http-server
    Preparing search index...

    Function createAuthPreHandler

    • Factory that creates a Fastify preHandler hook enforcing bearer-token authentication on any route whose URL matches one of the given glob patterns.

      The returned hook is intended to be registered globally via fastify.addHook("preHandler", createAuthPreHandler(authPaths)). For every request, request.routeOptions.url is tested against the compiled picomatch matcher; non-matching routes pass through unconditionally.

      When a route is protected, token verification is delegated to the verifyToken decorator registered on the Fastify instance.

      Parameters

      • authPaths: string[]

        Array of picomatch glob patterns (e.g. ["/api/**"]).

      • realm: string = "api"

        Protection-space label used in the WWW-Authenticate challenge (RFC 6750). Typically the Keycloak realm name. Defaults to "api".

      Returns (request: FastifyRequest, reply: FastifyReply) => Promise<void>