Skip to main content

clientInfo Hook

Type: onRequestHookHandler Access: nauth.middleware.clientInfo

Initializes AsyncLocalStorage context and extracts client information (IP, User-Agent).

Signature

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

Registration

fastify.addHook('onRequest', nauth.middleware.clientInfo);

Context Data

PropertyTypeDescription
ipstringClient IP address
userAgentstringRaw User-Agent header
deviceNamestringParsed device name
deviceTypestringmobile | desktop | tablet
platformstringOS platform
browserstringBrowser name

Proxy Trust

The hook reads the client IP from Fastify's request.ip. Fastify only populates request.ip from forwarding headers (X-Forwarded-For, etc.) when trustProxy is enabled in the Fastify server options. Without it, request.ip will be the IP of the last network hop (e.g. your load balancer), not the real client.

Enable trustProxy when creating the Fastify instance:

import Fastify from 'fastify';

const fastify = Fastify({
// Trust the first proxy in front of the app
trustProxy: true,

// Or trust specific IP addresses / CIDR ranges:
// trustProxy: '10.0.0.0/8',
// trustProxy: ['loopback', '10.0.0.0/8'],
});

fastify.addHook('onRequest', nauth.middleware.clientInfo);
// ...

See the Fastify server options documentation for all valid values.