Skip to main content

tokenDelivery Hook

Type: onSendHookHandler Access: nauth.middleware.tokenDelivery

Intercepts responses to set authentication cookies.

Signature

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

Registration

fastify.addHook('onSend', nauth.middleware.tokenDelivery);

Behavior

  • Detects AuthResponseDTO in response body
  • Sets accessToken and refreshToken cookies based on delivery mode
  • Removes tokens from JSON body when using cookie delivery
  • Sets CSRF token cookie

Configured via config.cookies:

OptionTypeDescription
securebooleanHTTPS only
httpOnlybooleanNo JS access
sameSitestringCSRF protection
domainstringCookie domain