fastify-xauth-jwks
getAuthEndpoint
Get the name of the registered path that authenticated the current request.
getAuthEndpoint
Get the logical name of the registered path that authenticated the current request. Reads request.auth.path. Returns null if the request is unauthenticated or request.auth is not set.
Signature
import { getAuthEndpoint } from "@xenterprises/fastify-xauth-jwks/utils";
getAuthEndpoint(request: FastifyRequest): string | null
Params
| Name | Type | Required | Description |
|---|---|---|---|
request | FastifyRequest | Yes | Authenticated Fastify request. Must have request.auth.path set by the plugin. |
Returns
string | null — The path name (e.g., "admin", "portal"), or null if the request is unauthenticated.
Throws
Does not throw.
Examples
Basic — log which auth path was used
import { getAuthEndpoint } from "@xenterprises/fastify-xauth-jwks/utils";
fastify.addHook("onSend", async (request) => {
const endpoint = getAuthEndpoint(request);
if (endpoint) {
fastify.log.info({ authEndpoint: endpoint }, "Request authenticated via path");
}
});
Realistic — differentiate behavior by auth path
import { getAuthEndpoint } from "@xenterprises/fastify-xauth-jwks/utils";
fastify.get("/shared/resource", async (request) => {
const endpoint = getAuthEndpoint(request);
if (endpoint === "admin") {
return db.resources.getAll({ includeDeleted: true });
}
if (endpoint === "portal") {
return db.resources.getAll({ userId: request.auth.userId });
}
return reply.code(403).send({ error: "Unrecognized auth path" });
});
See Also
- requireEndpoint — Prebuilt
preHandlerthat enforces a specific auth path - getUserId — Get the user ID from
request.auth.userId
AI Context
package: "@xenterprises/fastify-xauth-jwks"
import: getAuthEndpoint from "@xenterprises/fastify-xauth-jwks/utils"
use-when: Get the name of the JWKS path that authenticated the current request (from request.auth.path)
returns: string | undefined
