The Fastify package
For: a developer of a Fastify 5 app on Node 22.18 or newer.
What it does: this page lists the exports of the package, the steps of the mount, and the paths that the protect rule gets. The export check holds this page to packages/fastify/src/index.ts.
Before you start
Section titled “Before you start”- Read The API reference for the auth object that the package takes.
- Mount core:
await app.register(authRoutes({ auth })). - Add the field:
app.decorateRequest("authSession", null). - Read the session:
app.addHook("onRequest", authSession({ auth })), thengetSession(request)in a route. - Protect paths:
authSession({ auth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }).
Exports
Section titled “Exports”| Export | What it does |
|---|---|
authRoutes |
Answers the plugin of the routes of core. In its scope no parser of Fastify runs, so core reads the body as it came. |
authSession |
Answers the onRequest hook that sets request.authSession. It sends a person with no session on a protected path to the sign-in page, and adds the renewed cookie. |
getSession |
Answers the session that the hook set, or null. |
toWebRequest |
Answers the web request of a Fastify request of the route plugin. |
sendWebResponse |
Writes a web response to Fastify, with each Set-Cookie line. |
AuthSource |
The auth instance, or a function that makes it at the first request. |
FastifyRoutesOptions |
The options of authRoutes: auth, basePath, and waitUntil. waitUntil keeps work alive after the answer. It has no default. |
FastifySessionOptions |
The options of authSession: auth, protect, signInPath, and basePath. |
The paths that protect gets
Section titled “The paths that protect gets”protectgets the path as the router of Fastify matched it, decoded. find-my-way decodes the path once and keeps%25.- So
/%61ccountreaches the rule as/account, and/caf%C3%A9as/café. - The path keeps the reserved escapes such as
%2F, and drops the query. It keeps dot segments and backslashes:/account/..stays/account/... - find-my-way stores a
%of a route as%25. So a route/x/%2eis the path/x/%252efor the rule. - The hook reads none of the router options, and fails closed. It runs the rule on each form that the router can match.
- These forms are the path as it is, and the path with duplicate slashes merged (
ignoreDuplicateSlashes). - They are also the path with the last slash cut (
ignoreTrailingSlash), and the path cut at the first;(useSemicolonDelimiter). - The hook also folds case: the rule runs on each form in lower case. find-my-way compares with
toLowerCasewhencaseSensitiveis false. - find-my-way compares the hex digits of a kept escape as text, so
/q%3fxand/q%3Fxreach the same route. - So the hook also runs the rule with each escape in upper case. The step does not change the text after
%25, so write that text in lower case. - Write the rule in lower case, with each kept escape in upper case:
%3F,%2F. - A path needs a session when the rule holds for one form. Prefer a prefix rule: a route with a parameter or a wildcard also matches paths under the prefix.
How it behaves
Section titled “How it behaves”- Node keeps a detached promise alive, so a plain server needs no
waitUntil. On a host that stops the function after the answer, passwaitUntilin the options ofauthRoutes. The SMS of a phone code and the flush of the events run under it (ruling 29 of plan 12i). - The package takes
fastify5.12.5 or a later 5 release as a peer dependency. - The routes of core read the body as it came. A form post works with no
@fastify/formbody. - The plugin passes the raw body stream to core, and Fastify buffers none of it. Core reads at most the cap of each route and answers 413
body_too_largewith its own headers. Fastify’sbodyLimittakes no part in the routes of core. - The field is
request.authSession, because@fastify/sessionownsrequest.session. - The hook reads no session for a route of core. Core reads and renews the session itself.
- The hook skips the session read only when the raw path and the matched path are both under the base path. Else it judges the request as a page.
- Core matches its routes on the raw path. A path that Fastify decodes to a path of core, such as
/%61uth/x, never runs a page. - When the rule holds for the decoded path and there is no session, the answer is the sign-in redirect. Else core answers 404.
- After the sliding refresh, the answer carries the renewed session cookie.
- Each
Set-Cookieline of core is added to the answer. A line that an earlier hook set stays. - The
nextvalue of the redirect is the path and the query of the request, as a URL reads them. - One decode by the router gives back the page that the person asked for. So
/account/a%2Fbreturns to/account/a%2Fb, and an escaped backslash stays escaped. - The origin of the redirect comes from the Host header. A request line in absolute form, such as
GET http://host/path, gives its path only. - A Host header that does not parse falls back to the origin of core’s
baseURL. So no request gets a 500 of the binding’s own. authRoutesmakes its route at registration. With a function inauth, the route is under “/auth” unless you setbasePath.- With an auth object in
auth, the route is under the base path of the auth object. - Behind a TLS proxy, set
trustProxyin the options of Fastify, so the sign-in redirect keeps https. toWebRequestandsendWebResponseare the two seams to Fastify.