docs: add Encore integration docs (#7760)

This commit is contained in:
Ivan Cernja
2026-03-17 18:50:00 +01:00
committed by Alex Yang
parent 6b921ed252
commit 208f6fdd27
4 changed files with 162 additions and 0 deletions

View File

@@ -41,3 +41,4 @@ electronjs
wagmi
tldts
headimgurl
encoredev

View File

@@ -0,0 +1,142 @@
---
title: Encore Integration
description: Integrate Better Auth with Encore.
---
Better Auth can be integrated with your [Encore](https://encore.dev) application (an open source TypeScript framework with automated infrastructure and observability).
Before you start, make sure you have a Better Auth instance configured. If you haven't done that yet, check out the [installation](/docs/installation).
### Getting Started
Install the Encore CLI and create a new application. This will scaffold a TypeScript project with the required structure:
```bash title="Terminal"
brew install encoredev/tap/encore # if you don't have Encore installed
encore app create my-app --example=ts/hello-world
cd my-app
npm install better-auth
```
### Mount the handler
To handle auth requests, mount Better Auth on a catch-all endpoint using Encore's `api.raw()`:
```ts title="auth/handler.ts"
import { api } from "encore.dev/api";
import { toNodeHandler } from "better-auth/node";
import { auth } from "./auth"; // Your Better Auth instance
export const authHandler = api.raw(
{ expose: true, path: "/api/auth/*path", method: "*" },
toNodeHandler(auth)
);
```
<Callout>
Encore's `api.raw()` provides Node.js request/response types. We use `toNodeHandler` from `better-auth/node` to bridge these to Better Auth's Web API handler.
</Callout>
### CORS
If your frontend runs on a different origin, configure CORS in your `encore.app` file to allow credentials (cookies) to be sent with requests:
```json title="encore.app"
{
"id": "your-app",
"global_cors": {
"allow_origins_with_credentials": ["http://localhost:3000"]
}
}
```
### Trusted Origins
When requests come from a different origin, they are blocked by default. Add trusted origins to your Better Auth config:
```ts title="auth/auth.ts"
export const auth = betterAuth({
trustedOrigins: ["http://localhost:3000", "https://your-app.com"],
// ... rest of config
});
```
### Local Development
Start your app with the Encore CLI. Make sure Docker is running as Encore uses it to manage local infrastructure:
```bash title="Terminal"
encore run
```
<Callout type="tip">
Open the local dashboard at `localhost:9400` to see traces for all requests, including auth handler execution and session validation. Useful for debugging auth issues.
</Callout>
### Protecting Endpoints
Encore has a built-in auth handler pattern for protecting endpoints. Create an auth handler that validates Better Auth sessions:
```ts title="auth/gateway.ts"
import { APIError, Gateway, Header } from "encore.dev/api";
import { authHandler } from "encore.dev/auth";
import { auth } from "./auth";
interface AuthParams {
authorization: Header<"Authorization">;
cookie: Header<"Cookie">;
}
interface AuthData {
userID: string;
email: string;
name: string;
}
const handler = authHandler(async (params: AuthParams): Promise<AuthData> => {
const headers = new Headers();
if (params.authorization) {
headers.set("Authorization", params.authorization);
}
if (params.cookie) {
headers.set("Cookie", params.cookie);
}
const session = await auth.api.getSession({ headers });
if (!session?.user) {
throw APIError.unauthenticated("invalid session");
}
return {
userID: session.user.id,
email: session.user.email,
name: session.user.name,
};
});
export const gateway = new Gateway({ authHandler: handler });
```
Then protect any endpoint with `auth: true`:
```ts
import { api } from "encore.dev/api";
import { getAuthData } from "~encore/auth";
export const getProfile = api(
{ expose: true, auth: true, method: "GET", path: "/profile" },
async () => {
const authData = getAuthData()!;
return { id: authData.userID, email: authData.email };
}
);
```
<Callout type="tip">
If you want to manage session cookies directly in your Encore endpoints, check out Encore's [typed cookie support](https://encore.dev/docs/ts/primitives/cookies).
</Callout>
### Learn More
For a complete walkthrough including database setup and deployment, see the [Better Auth with Encore tutorial](https://encore.dev/blog/betterauth-tutorial).

View File

@@ -176,6 +176,19 @@ export const Icons = {
</g>
</svg>
),
encore: () => (
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
height="1em"
viewBox="90.6 91 90.9 102.1"
>
<path
className="fill-foreground"
d="M181.4,170.2v22.9H90.6v-69.3c14.4-3.1,28.7-7.1,42.6-12c16.6-5.8,32.7-12.7,48.3-20.8v25.6 c-13.2,6.4-26.9,12-40.8,16.9c-15.7,5.5-31.8,9.9-48.1,13.3v0.2c30.1-2.8,59.7-7.7,88.9-14.5v23.5c-29.2,6.6-58.9,11.3-88.9,14v0.2 H181.4z"
/>
</svg>
),
nestJS: (props?: SVGProps<any>) => (
<svg
className={props?.className}

View File

@@ -1522,6 +1522,12 @@ C0.7,239.6,62.1,0.5,62.2,0.4c0,0,54,13.8,119.9,30.8S302.1,62,302.2,62c0.2,0,0.2,
icon: Icons.fastify,
href: "/docs/integrations/fastify",
},
{
title: "Encore",
icon: Icons.encore,
href: "/docs/integrations/encore",
isNew: true,
},
{
title: "Express",
icon: Icons.express,