Radon
Framework Integrations

Fastify

Register Radon as a Fastify plugin — all auth routes under a prefix, plus a requireAuth preHandler for your own routes.

Radon ships as a Fastify plugin. Register radonFastify(auth) with a prefix, and guard your own routes with the requireAuth preHandler.

1. Register the auth routes

server.ts
import Fastify from "fastify";
import { radonFastify } from "@radonsdk/auth/integrations/fastify";
import { auth } from "./auth";

const app = Fastify();

// Serves every auth route under /api/auth.
await app.register(radonFastify(auth), { prefix: "/api/auth" });

await app.listen({ port: 3000 });

The prefix carries the base path

Radon's routes are relative to the plugin, so the Fastify prefix is what puts them under /api/auth. No basePath option is needed here.

2. Guard your own routes

requireAuth(auth) is a preHandler hook. It rejects unauthenticated requests with 401 and attaches req.radonUser (and req.radonUserId) on success.

import { radonFastify, requireAuth } from "@radonsdk/auth/integrations/fastify";

app.get("/me", { preHandler: requireAuth(auth) }, (req) => {
  return req.radonUser; // present because the preHandler passed
});

Options

await app.register(radonFastify(auth, {
  cookieName: "acme_session",
  successRedirect: "/dashboard",
  failureRedirect: "/signin?error=1",
}), { prefix: "/api/auth" });

TypeScript: typing req.radonUser

Augment Fastify's request type in a .d.ts: declare module "fastify" { interface FastifyRequest { radonUser?: RadonUser; radonUserId?: string } }.

CSRF

import { issueCsrfToken, verifyCsrfToken } from "@radonsdk/auth/integrations/fastify";

Next steps

On this page