Better Auth plugin
Wire Better Auth into Reion with @reionjs/better-auth, route handlers, and optional ctx.auth helpers.
Authentication is provided by @reionjs/better-auth, which connects Better Auth to Reion's HTTP stack.
Install
From your app root:
npx reion add -p @reionjs/better-authyarn dlx reion add -p @reionjs/better-authbunx reion add -p @reionjs/better-authpnpm exec reion add -p @reionjs/better-authThe setup script will:
- Prompt for a database option: Simple (SQLite file), PostgreSQL, MySQL, or Manual (in-memory adapter until you plug in your own database).
- Install
better-authand, when you did not pass--skip-install, the driver packages for your choice (for examplebetter-sqlite3and@types/better-sqlite3for SQLite). - Scaffold
src/plugins/better-auth.tsand a catch-all auth route atsrc/router/api/auth/[[...params]]/route.ts. - Register the plugin in
reion.config.ts(including addingplugins: []if it was missing).
Use reion add --skip-install only if you install dependencies yourself; you may then need to run the package manager commands the setup prints for database drivers.
Add @reionjs/better-auth and better-auth:
npm install @reionjs/better-auth better-authyarn add @reionjs/better-auth better-authbun add @reionjs/better-auth better-authpnpm add @reionjs/better-auth better-authAdd your database client the same way (for example better-sqlite3 and @types/better-sqlite3, or pg, mysql2, etc.).
Then add the auth route, plugin module, and reion.config.ts entries yourself, following the examples below.
Auth HTTP route
The generated route forwards all methods to Better Auth's Node handler and opts into the raw body so Better Auth can read the request correctly:
import { createBetterAuthRouteHandler } from "@reionjs/better-auth";
import { auth } from "@/plugins/better-auth.js";
const handleAuth = createBetterAuthRouteHandler(auth);
export const RAW_BODY = true;
export const GET = handleAuth;
export const POST = handleAuth;
export const PUT = handleAuth;
export const PATCH = handleAuth;
export const DELETE = handleAuth;
export const OPTIONS = handleAuth;Mount path follows your folder structure (here: /api/auth/*). Adjust the router path if you need a different prefix.
Plugin registration
Your better-auth instance and plugin export usually live in src/plugins/better-auth.ts. Register the plugin in reion.config.ts:
import { betterAuthPlugin } from "./src/plugins/better-auth";
export default {
plugins: [betterAuthPlugin],
};(Exact import path may differ if setup used a different layout.)
BetterAuthPlugin options
BetterAuthPlugin({
auth, // required: return value of betterAuth({ ... })
attachToContext?: boolean; // default true
});auth— your Better Auth app instance.attachToContext— whentrue(default), the plugin setsctx.authandctx.authSession()on each request so non-auth routes can read the session. Set tofalseif you only want the/api/authroutes and no context helpers.
Types for ctx.auth / ctx.authSession come from the package's module augmentation; importing BetterAuthPlugin (or anything) from @reionjs/better-auth loads that augmentation.
Session in a normal route
export async function GET(ctx) {
const session = await ctx.authSession();
if (!session) {
ctx.res.status(401);
return { error: "Unauthorized" };
}
ctx.res.status(200);
return { user: session.user };
}Use ctx.auth.api for other Better Auth server APIs as needed.