Core reads the plugins you pass to betterAuth({ plugins }). It has no runtime dependency on better-auth and adds no tables.
Core
The glue for apps that run two or more BYO packages.
Core is not a feature. It is a small library that gives your BYO packages one event bus, one cron route and one org-cleanup call, so you wire each once instead of once per package. It adds no tables and no endpoints, and nothing goes in plugins: []. With a single BYO package you don't need it.
npm i @byo-oss/coreimport { createEventBus } from "@byo-oss/core";
const onEvent = createEventBus([
(event) => activity.record(event),
async (event) => {
if (event.type === "support.agent.replied" && event.userId) {
await auth.api.notify({ body: { userId: event.userId, type: event.type, title: "New reply" } });
}
},
]);
export const auth = betterAuth({
plugins: [support({ onEvent }), feedback({ onEvent })],
});Relies on
peerDependencies · @byo-oss/core 0.2.0Call the cleanup fan-out from its organizationHooks when an organization is deleted or a member is removed.
Wire it once, not once per package.
Each BYO package that sends events, runs background work or stores per-organization data would otherwise need its own wiring. Core reads the plugins you already pass to betterAuth({ plugins }) and calls the hooks they declare.
One event bus
createEventBus turns a list of consumers into the one onEvent you pass to every BYO plugin. A consumer that throws is reported, and the next one still runs.
One cron route
sweepAll runs every plugin's background work in plugin order, inside a time budget you set, and tells you when to call again.
One cleanup call
deleteOrganizationData and deleteMemberData reach every plugin that stores per-organization or per-member data.
Nothing is skipped
A plugin that fails never stops the others. Cleanup runs all plugins, then throws a ByoFanOutError naming the ones that failed.
Joins your transaction
Core opens no transaction. Call the fan-out inside yours and every plugin's cleanup runs on it, and rolls back with it.
A contract for plugin authors
A plugin declares sweep and cleanup hooks under a byo key, and core reaches them. better-auth ignores the key.
Three steps.
For an app that already runs two or more BYO packages.
- step 01
Build one event bus
Pass the same onEvent to every BYO plugin that emits events.
lib/auth.tsimport { createEventBus } from "@byo-oss/core"; const onEvent = createEventBus([ (event) => activity.record(event), async (event) => { if (event.type === "support.agent.replied" && event.userId) { await auth.api.notify({ body: { userId: event.userId, type: event.type, title: "New reply" } }); } }, ]); export const auth = betterAuth({ plugins: [support({ onEvent }), feedback({ onEvent })], }); - step 02
Add one cron route
Run it every minute from any scheduler, and protect it the way your scheduler expects.
app/api/cron/route.tsimport { sweepAll } from "@byo-oss/core"; export async function GET() { const result = await sweepAll(auth, { budgetMs: 20_000 }); return Response.json(result); } - step 03
Wire the cleanup hooks
From the organization plugin's hooks, so every package cleans up when an organization or member goes.
lib/auth.tsimport { deleteMemberData, deleteOrganizationData } from "@byo-oss/core"; organization({ organizationHooks: { afterDeleteOrganization: async ({ organization }) => { await deleteOrganizationData(auth, organization.id); }, afterRemoveMember: async ({ member }) => { await deleteMemberData(auth, { userId: member.userId, organizationId: member.organizationId }); }, }, });
Best-effort events, all-plugins cleanup.
In-process and best-effort. The bus never fails the request that emitted the event, and nothing is retried or stored.
Core adds no guarantee of its own. Leases, retries and at-least-once delivery belong to each plugin's sweep.
All plugins run, then it throws if any failed. Outside a transaction, the cleanups that succeeded stay done.
Declare hooks under byo.
A plugin declares its sweep and cleanup hooks under a byo key, and core's helpers call them. better-auth ignores the key at runtime.
- End the plugin with satisfies ByoPlugin<BetterAuthPlugin>
- Every hook is optional: declare only what the plugin has
- Read the adapter with getCurrentAdapter, so the hook joins the caller's transaction
- Keep exporting your own cleanup functions and sweep endpoint
import type { BetterAuthPlugin } from "better-auth";
import type { ByoPlugin } from "@byo-oss/core";
export const feedback = (options: FeedbackOptions = {}) =>
({
id: "feedback",
endpoints: { /* ... */ },
byo: {
sweep: (auth) => processFeedbackDigests(auth),
deleteOrganizationData: async (auth, organizationId) => {
await deleteFeedbackOrgData(auth, organizationId);
},
deleteMemberData: async (auth, { userId, organizationId }) => {
await deleteFeedbackMemberData(auth, { userId, organizationId });
},
},
}) satisfies ByoPlugin<BetterAuthPlugin>;Glue, not infrastructure.
Core does not replace a scheduler or a queue. There is also no exit to a third-party tool: remove it and each plugin keeps its own sweep endpoint and cleanup functions, which you then call yourself.
You still need one cron that calls sweepAll.
No outbox, no retries, no cross-process delivery.
User data goes through foreign keys and each plugin's own databaseHooks. There is no user-delete fan-out.
No data export fan-out, and no shared identify(request) helper yet.