X Enterprises
fastify-xauth-nile

getSession(request)

Resolves the Nile session for an incoming request without running middleware.

getSession(request)

Resolves the Nile session for an incoming Fastify request by running nile.auth.getSession() inside a request-scoped context built from the request's headers (which carry the session cookie). Returns the session object, or undefined when the request is unauthenticated.

Do not pass the Fastify request object to nile.auth.getSession. Always use this helper (or GET /api/auth/session).

Signature

instance.getSession(request: FastifyRequest): Promise<object | undefined>

Params

NameTypeRequiredDescription
requestFastifyRequestYesThe incoming Fastify request (headers are forwarded to the SDK)

Returns

Promise<object | undefined> — the Nile session (including session.user when present), or undefined when there is no valid session.

Throws

Propagates errors thrown by the Nile SDK (e.g. network failures against the nile-auth API).

Examples

Conditional logic without middleware

const auth = fastify.xAuthNile.get("api");

fastify.get("/api/session", async (request, reply) => {
  const session = await auth.getSession(request);
  if (!session) {
    return reply.code(401).send({ error: "Unauthorized" });
  }
  return { user: session.user };
});

Inside a custom hook

fastify.addHook("onRequest", async (request) => {
  request.session = await fastify.xAuthNile.default.getSession(request);
});

See also

AI Context

package: "@xenterprises/fastify-xauth-nile"
method: fastify.xAuthNile.get(name).getSession(request)
use-when: resolve the Nile session inside a handler/hook without requireAuth() middleware; undefined when unauthenticated
usage: const session = await fastify.xAuthNile.default.getSession(request)
Copyright © 2026