@getruba/better-auth lets Better Auth identity become the customer key used across Ruba. Enable only the surfaces your application needs:
Install the identity bridge
Add Better Auth, the Ruba plugin, and the core SDK:- npm
- yarn
- pnpm
- bun
Terminal
Establish the shared customer identity
1
Create a server credential
Generate an Organization Access Token in Ruba settings and keep it in the server environment.
.env
2
Compose the server plugins
Create one Ruba SDK client, pass it to
ruba(), and select the capability modules under use:checkoutstarts buying flows.portalexposes customer self-service and records.usagesends events and reads meters.webhooksreceives signed state changes.
auth.ts
Root configuration
3
Expose typed client methods
Add
rubaClient() to the browser-facing Better Auth client. Billing authority still remains in the server configuration.auth-client.ts
Keep user and customer lifecycle aligned
EnablecreateCustomerOnSignUp to create a customer with the Better Auth user ID stored as externalId. That stable key removes the need for a separate mapping table.
For deletion, enable Better Auth’s deleteUser flow and remove the external Ruba customer from afterDelete:
Customer Deletion Example
Start checkout from an authenticated session
Source code Addcheckout under use. Its configuration separates catalog mapping from browser behavior:
1
Register checkout on the server
Place the configured
checkout() module in the root Ruba plugin’s use array.Checkout Plugin Example
2
Invoke it from the client
Call With Better Auth Organizations, use the organization ID as
authClient.checkout() with product IDs or one configured slug. referenceId is copied into checkout, order, and subscription metadata.BetterAuth Checkout with Ruba Example
referenceId to associate the resulting commerce records with that team rather than only the individual session.BetterAuth Checkout with Ruba Organization Example
Connect authenticated usage to meters
Source code Theusage module adds event ingestion and customer-meter reads. Register it in use after the application’s usage model exists.
Usage Plugin Example
Record authoritative activity
With signup sync enabled,session.user.id is already the customer’s externalId. Reuse the configured SDK client inside the protected work route:
app/api/ai/video/route.ts (Next.js App Router)
name, the Better Auth user ID as externalCustomerId, and optional string, number, or boolean metadata used by filters and aggregations.
Non-authoritative browser signals
authClient.usage.ingestion(...) sends through the Better Auth server and attaches the signed-in user automatically:
auth-client.ts
Read the user’s meter position
authClient.usage.meters() lists Customer Meters for the active session. page starts at 1 and limit caps each page.
Customer Meters with Usage Plugin Example
Bring asynchronous changes into Better Auth
Source code The webhook module owns/api/auth/ruba/webhooks, verifies delivery, and routes events to your callbacks.
1
Register the generated endpoint
In Ruba webhook settings, create an endpoint ending in
/api/auth/ruba/webhooks.2
Store its secret
Save the issued value as
RUBA_WEBHOOK_SECRET in the server environment..env
3
Bind callbacks
Add
webhooks() under use, pass the secret, and implement the events your application consumes.Webhooks Plugin Example
Choose callback granularity
UseonPayload as a catch-all or register callbacks around the domain your application owns:
Query commerce through the signed-in customer
Source codePortal module
authClient.customer.
Open self-service
authClient.customer.portal() redirects the user to Ruba’s portal for orders, subscriptions, and benefits.
Open Customer Portal Example
Fetch one access snapshot
authClient.customer.state() returns identity, direct active subscriptions, granted benefits, and active meters with their balances.
Retrieve Customer State Example
List individual record types
Granted benefits
The benefits method returns grants belonging to the active customer.List User Benefits Example
Orders
The orders method returns that customer’s purchases and renewal records.List User Orders Example
referenceId and use that reference to find the organization’s subscriptions.
List Organization Subscriptions Example
Subscriptions
The subscriptions method returns relationships tied directly to the active customer.List User Subscriptions Example
Direct-customer listing does not include subscriptions purchased through a parent organization.