Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions docs/integrations/better-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,24 @@ export const auth = betterAuth({

## Handler

After setting up Better Auth instance, we can mount to Elysia via [mount](/patterns/mount.html).
After setting up Better Auth instance, we can mount to Elysia via [all](/essential/route.html#all-method).

We need to mount the handler to Elysia endpoint.

:::note
Using `all` can prevent unwanted response interception by Better Auth and keep the integrity of OAuth callback URLs.

The `all` method does not rewrite the path headers, so we have to manually set the URL here.
:::
Comment thread
hexadecimal233 marked this conversation as resolved.

```ts [index.ts]
import { Elysia } from 'elysia'
import { auth } from './auth'

// The default better-auth Base URL is "/api/auth"
const app = new Elysia()
.mount(auth.handler) // [!code ++]
.listen(3000)
.all("/api/auth*", ({ request }) => auth.handler(request)) // [!code ++]
.listen(3000)

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
Expand All @@ -56,23 +63,19 @@ Then we can access Better Auth with `http://localhost:3000/api/auth`.

### Custom endpoint

We recommend setting a prefix path when using [mount](/patterns/mount.html).

```ts [index.ts]
import { Elysia } from 'elysia'

const app = new Elysia()
.mount('/auth', auth.handler) // [!code ++]
.listen(3000)
.all("/v1/auth*", ({ request }) => auth.handler(request)) // [!code ++]
.listen(3000)

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
)
```

Then we can access Better Auth with `http://localhost:3000/auth/api/auth`.

But the URL looks redundant, we can customize the `/api/auth` prefix to something else in Better Auth instance.
Then modify the `basePath` in our Better Auth file.

```ts
import { betterAuth } from 'better-auth'
Expand All @@ -81,14 +84,13 @@ import { passkey } from 'better-auth/plugins/passkey'

import { Pool } from 'pg'

// MUST MATCH! Or social login may break with 404 errors.
export const auth = betterAuth({
basePath: '/api' // [!code ++]
basePath: '/v1/auth' // [!code ++]
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
```

Then we can access Better Auth with `http://localhost:3000/auth/api`.

Unfortunately, we can't set `basePath` of a Better Auth instance to be empty or `/`.
Then we can access Better Auth with `http://localhost:3000/v1/auth`.

## OpenAPI

Expand Down