Better Auth
Better Auth keeps the session in a cookie. When your API shares the cookie, HyperFetch
only has to send it: on the same origin that happens on its own, on another origin you turn on credentials, and on
the server you forward the incoming cookie header. An API that expects a bearer token gets a short-lived JWT from the
jwt plugin instead.
- Cookies first: same-origin requests carry the session with no code at all.
- Across origins: one adapter default sends the cookie to an API on another origin.
- Server rendering: Server Components forward the user's cookie to the API.
- JWT for other services:
authClient.token()gives a token that any backend checks against the JWKS endpoint.
Install
- npm
- yarn
- pnpm
npm install @hyper-fetch/core better-auth
yarn add @hyper-fetch/core better-auth
pnpm add @hyper-fetch/core better-auth
Same origin: nothing to add
When the API runs on the same origin as the app, for example Next.js route handlers or a proxy under /api, the browser
sends the Better Auth cookie with every fetch. Create the client and send requests as usual:
import { createClient } from "@hyper-fetch/core";
export const client = createClient({ url: "/api" });
export const getProjects = client.createRequest<{
response: { id: string; name: string }[];
}>()({ endpoint: "/projects" });
On the API, read the session from the request headers with auth.api.getSession({ headers }).
API on another origin
Browsers send cookies to another origin only when the request asks for it. Turn on credentials: "include" for every
request of the client:
export const client = createClient({ url: "https://api.example.com" });
client.adapter.setAdapterDefaults(() => ({ credentials: "include" }));
Three settings on the other side make it work:
- Add the app's origin to
trustedOriginsin your Better Auth config, or Better Auth rejects its requests. - Answer with
Access-Control-Allow-Credentials: trueand the app's origin inAccess-Control-Allow-Origin, not*. - Better Auth cookies are host-only with
SameSite=Lax. Forapp.example.comandapi.example.com, turn oncrossSubDomainCookies. Fully separate domains need the JWT instead.
Send requests from the server
A Server Component has no browser cookies. Forward the ones from the incoming request:
import { headers } from "next/headers";
import { getProjects } from "@/lib/api";
export default async function ProjectsPage() {
const cookie = (await headers()).get("cookie") ?? "";
const { data } = await getProjects.setHeaders({ cookie }).send();
return (
<ul>
{data?.map((project) => (
<li key={project.id}>{project.name}</li>
))}
</ul>
);
}
A server request needs a full URL, so give the client an absolute url on the server. To read the user without calling
the API, use await auth.api.getSession({ headers: await headers() }).
Send a JWT to another service
Some backends can't read your cookie: a service on another domain, a Python API or a worker. Add the jwt plugin to
the server and jwtClient() to the auth client, then send the token it issues as a bearer token:
import { createAuthClient } from "better-auth/react";
import { jwtClient } from "better-auth/client/plugins";
export const authClient = createAuthClient({ plugins: [jwtClient()] });
client.onAuth(async (request) => {
const { data } = await authClient.token();
if (!data?.token) return request;
return request.setHeaders({
...request.headers,
Authorization: `Bearer ${data.token}`,
});
});
The token lasts 15 minutes by default, so cache it for a few minutes if you send many requests. The backend verifies it
with the public keys at /api/auth/jwks, for example with jose:
import { createRemoteJWKSet, jwtVerify } from "jose";
const jwks = createRemoteJWKSet(
new URL("https://app.example.com/api/auth/jwks"),
);
const { payload } = await jwtVerify(token, jwks, {
issuer: "https://app.example.com",
audience: "https://app.example.com",
});
The issuer and the audience default to your Better Auth base URL. The bearer plugin also sends the session token in
a header, but Better Auth recommends it only for clients that can't store cookies.
Sign out
Clear the HyperFetch client after Better Auth ends the session, so no cached data of the previous user stays behind:
await authClient.signOut();
client.clear();
- Apps whose API runs on the same origin or the same site as the app.
- Next.js pages that fetch with the user's cookie on the server.
- Separate services that should trust Better Auth users without sharing a database.
- Same-origin requests send the Better Auth cookie with no extra code.
setAdapterDefaultswithcredentials: "include",trustedOriginsand CORS cover another origin.- Server code forwards the
cookieheader withsetHeaders. - The
jwtplugin issues bearer tokens that any backend verifies against the JWKS endpoint.