Better Auth

Let people who sign in with Better Auth call your API. Its JWT plugin signs a token, and your API verifies it against the plugin's public keys.

Supports
Better Auth with the JWT plugin

Better Auth keeps sessions in a cookie on your app's own domain. An API on another server or another domain cannot read that cookie. Better Auth's JWT plugin signs a short-lived token for the session instead, and your API verifies it with @kizunajs/jwt.

pnpm add @kizunajs/jwt@beta

Turn on the JWT plugin

Name your API as the token's audience. Without it, the audience is your app's own URL:

auth.ts
import { betterAuth } from 'better-auth';
import { jwt } from 'better-auth/plugins';

export const auth = betterAuth({
    baseURL: 'https://app.example.com',
    plugins: [
        jwt({
            jwt: {
                audience: 'https://api.example.com',
            },
        }),
    ],
});

The plugin serves its public keys at /api/auth/jwks and signs with EdDSA. A token lasts 15 minutes.

Declare the identity

The issuer is your app's origin, the baseURL above:

src/identities.ts
import { verifyJwt } from '@kizunajs/jwt';

export const user = k.identity
    .bearer({
        bearerFormat: 'JWT',
        context: z.object({
            userId: z.string(),
            email: z.string(),
        }),
    })
    .guard(
        verifyJwt({
            jwks: 'https://app.example.com/api/auth/jwks',
            issuer: 'https://app.example.com',
            audience: 'https://api.example.com',
            algorithms: ['EdDSA'],
            claims: z.object({
                sub: z.string(),
                email: z.string(),
            }),
            context: ({ claims }) => ({
                userId: claims.sub,
                email: claims.email,
            }),
        })
    );

sub is the user's id, and the other claims are the session's user, like email and name.

Send the token

The app asks Better Auth for a token, and sends it before every request:

src/lib/api-client.ts
import { authClient } from './auth-client';
import { createClient } from './api-client.generated';

export const apiClient = createClient({
    baseUrl: 'https://api.example.com',
    onRequest: async ({ headers }) => {
        const { data } = await authClient.token();
        if (data) headers.set('Authorization', `Bearer ${data.token}`);
    },
});

Each call asks for a fresh token. Keep the token until shortly before its exp to save the round trip.

Roles

A role on the user, like the one Better Auth's admin plugin adds, arrives as a claim:

src/identities.ts
claims: z.object({
    sub: z.string(),
    role: z.enum(['member', 'admin']).default('member'),
}),
context: ({ claims }) => ({
    userId: claims.sub,
    role: claims.role,
}),

To choose the claims yourself, set jwt.definePayload on the plugin.

Other clients and AI assistants

For apps you do not own, like an MCP client, Better Auth's OAuth provider issues tokens with scopes. OAuth covers that setup.

Reference