Better Auth Fastify Integration Guide
This guide provides step-by-step instructions for configuring both essential handlers and CORS settings.
A configured Better Auth instance is required before proceeding. If you haven't set this up yet, please consult our Installation Guide.
Prerequisites
Verify the following requirements before integration:
- Node.js Environment: v16 or later installed
- ES Module Support: Enable ES modules in either:
package.json
:{ "type": "module" }
- TypeScript
tsconfig.json
:{ "module": "ESNext" }
- Fastify Dependencies:
For TypeScript: Ensure your
tsconfig.json
includes "esModuleInterop": true
for optimal compatibility. Authentication Handler Setup
Configure Better Auth to process authentication requests by creating a catch-all route:
Trusted origins
When a request is made from a different origin, the request will be blocked by default. You can add trusted origins to the auth
instance.
Configuring CORS
Secure your API endpoints with proper CORS configuration:
Always restrict CORS origins in production environments. Use environment variables for dynamic configuration.