
Fastify Better Auth Bridge
- 11 installs
- 22 repo stars
- Updated May 28, 2026
- acedergren/agentic-tools
fastify-better-auth-bridge is a Claude Code skill that wires Better Auth session resolution into Fastify 5 via the onRequest hook.
About
fastify-better-auth-bridge is a Claude Code skill that wires Better Auth session resolution into Fastify 5 via the onRequest hook. It covers building the Web Request bridge that forwards cookies, per-request Symbol-backed decorators to avoid shared array state, and patching missing IDCS org context. It is for cases where Better Auth already exists but Fastify lacks the framework bridge. Developers use it to fix Fastify session resolution and cookie forwarding.
- Wires Better Auth session resolution into Fastify 5 via an onRequest hook
- Handles the non-obvious cookie-forwarding, per-request decorator, and IDCS org-context patches
- Resolves identity in the bridge while leaving RBAC enforcement to route guards
Fastify Better Auth Bridge by the numbers
- 11 all-time installs (skills.sh)
- Ranked #3,574 of 4,347 Backend & APIs skills by installs in the Skillselion catalog
- Data as of Jul 28, 2026 (Skillselion catalog sync)
fastify-better-auth-bridge capabilities & compatibility
- Capabilities
- auth integration · session resolution · cookie forwarding · request decoration
- Works with
- oracle
- Use cases
- api development · debugging
- Pricing
- Free
What fastify-better-auth-bridge says it does
Wire Better Auth session resolution into Fastify 5 via the `onRequest` hook. Use this when Better Auth already exists but Fastify lacks the framework bridge.
Never build a parallel session layer when cookie header forwarding is all that's needed
Never skip the Web `Request` bridge — `auth.api.getSession()` requires a native Web API `Request`, not a Fastify request object.
npx skills add https://github.com/acedergren/agentic-tools --skill fastify-better-auth-bridgeAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 11 |
|---|---|
| repo stars | ★ 22 |
| Last updated | May 28, 2026 |
| Repository | acedergren/agentic-tools ↗ |
What it does
Wire Better Auth session resolution into Fastify 5 via an onRequest hook, forwarding cookies and patching IDCS org context.
Who is it for?
Bridging an existing Better Auth setup into Fastify 5 so sessions resolve and auth cookies forward correctly.
Skip if: Callback-URL or trusted-origin configuration, IDCS group mapping, or building a parallel session system alongside Better Auth.
When should I use this skill?
You need to bridge Better Auth into Fastify, fix Fastify session resolution, forward auth cookies, or decorate request auth.
What you get
A Fastify 5 onRequest bridge that forwards cookies, decorates per-request auth state, and patches missing IDCS org context.
- Fastify onRequest bridge that resolves Better Auth sessions
- Per-request decorated request.user/session/permissions
By the numbers
- 5-step required bridge shape
- version 2.0.0 per frontmatter
Files
Fastify Better Auth Bridge
Wire Better Auth session resolution into Fastify 5 via the onRequest hook. Use this when Better Auth already exists but Fastify lacks the framework bridge.
Do NOT load this skill when
- The problem is callback URL configuration, trusted origins, or provider bootstrap order
- The problem is IDCS group mapping or
org_membersprovisioning rules - The goal is to build a parallel session system alongside Better Auth
NEVER
- Never build a parallel session layer when cookie header forwarding is all that's needed — the symptom (no session) and the fix (forward cookies) are separated by two layers, making it easy to misdiagnose.
- Never enforce RBAC policy inside the bridge
onRequesthook — the bridge resolves identity, route guards enforce access. Mixing them makes both untestable. - Never share mutable array decorator defaults in Fastify 5 — arrays on the prototype are shared across all requests; use a Symbol-backed getter/setter per-request.
- Never skip the Web
Requestbridge —auth.api.getSession()requires a native Web APIRequest, not a Fastify request object. Passing the wrong type silently returns no session. - Never assume
reply.send(undefined)is safe in Fastify 5 — it throws.
The Non-Obvious Parts
Why cookie forwarding breaks silently
Missing cookie headers make login and session resolution fail in completely different places — the login succeeds (sets cookie in browser) but the next request has no session. There's no error; the session is simply null. The bridge and the login flow look unrelated. Always verify cookies are included in the Web Request you build.
Why Fastify 5 decorator arrays share state
fastify.decorateRequest('permissions', []) — the [] is a prototype default shared across all requests. First request mutates it; second request sees the previous request's permissions. Use a Symbol-keyed getter/setter that allocates a fresh array per request.
Why IDCS users have no active org
IDCS-provisioned users are created via group sync, not Better Auth's organization-switch flow. Their session has no activeOrganizationId. Downstream code expecting org context will silently receive undefined. Patch by querying org_members when activeOrganizationId is absent.
Required Bridge Shape
Step 1: Web `Request` bridge Build from protocol + hostname + URL + method + full incoming headers. The headers must include cookie — that's the only way auth.api.getSession() can resolve a session.
Step 2: Decorate request state once Standardize: request.user, request.session, request.permissions, request.apiKeyContext. Use Symbol-backed getter/setter for any field that holds an array or mutable object.
Step 3: Path exclusions Skip session resolution only for: health, metrics, Better Auth handler routes. Normalize query strings and trailing slashes before matching — inconsistent normalization creates auth misses on some routes only.
Step 4: Resolve session, then continue as anonymous on error If auth.api.getSession() throws: log it, continue with request.user = null. Never reject the request at bridge level — that's the route guard's job.
Step 5: Patch org context If request.session.activeOrganizationId is absent, query org_members by user ID and set the org context downstream. Only do this when org context is genuinely absent — not as a default fallback on every request.
Diagnostic Script
node scripts/check-fastify-auth-bridge.js /path/to/auth-plugin.ts
# Defaults to apps/api/src/plugins/auth.ts if no argument givenArguments
$ARGUMENTS: Optional path to the Fastify auth plugin file to inspect. Empty = use repo-default path (apps/api/src/plugins/auth.ts).
# No environment variables are required for this skill.
.env
#!/usr/bin/env node
const fs = require('node:fs');
const path = require('node:path');
const target = process.argv[2]
? path.resolve(process.argv[2])
: path.resolve(process.cwd(), 'apps/api/src/plugins/auth.ts');
if (!fs.existsSync(target)) {
console.error(`File not found: ${target}`);
process.exit(1);
}
const source = fs.readFileSync(target, 'utf8');
const checks = [
['web request helper', /function\s+toWebRequest\s*\(/],
['getSession usage', /auth\.api\.getSession/],
['request.user decorator', /decorateRequest\('user'/],
['request.session decorator', /decorateRequest\('session'/],
['request.permissions decorator', /decorateRequest\('permissions'/],
['exclude path handling', /excludePaths|excludeSet/],
['org membership fallback', /org_members|resolveUserOrgMembership/],
];
console.log(`Fastify Better Auth bridge check: ${target}`);
console.log('');
let failures = 0;
for (const [label, pattern] of checks) {
const ok = pattern.test(source);
console.log(`${ok ? '✓' : '✗'} ${label}`);
if (!ok) failures += 1;
}
if (failures > 0) {
console.error('');
console.error(`Bridge check failed with ${failures} missing pattern(s).`);
process.exit(1);
}
console.log('');
console.log('Bridge shape looks complete.');
Related skills
FAQ
Why does the session come back null after login?
Missing cookie headers in the Web Request break session resolution silently; the bridge must include the cookie header so auth.api.getSession() can resolve.
Why not share a Fastify array decorator default?
In Fastify 5, decorateRequest('permissions', []) shares one prototype array across requests; use a Symbol-backed getter/setter for a fresh array per request.