Better Auth Sync Plugin for Webhooks

0.2.0 · active · verified Wed Apr 22

better-auth-sync is a plugin for the Better Auth system, designed to synchronize authentication data to external databases via webhooks. It offers first-class integration and helper functions specifically for Convex, enabling real-time mirroring of user and session data. The current stable version is 0.2.0, indicating it is an early-stage but actively developed package. While a specific release cadence isn't defined, its low version number suggests ongoing evolution and potential for rapid iteration. Key differentiators include its tight integration with Better Auth's plugin architecture and specialized utilities for Convex, such as schema definitions, JWT handling, and webhook verification, streamlining complex authentication setups involving external data stores.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to configure Better Auth with the `better-auth-sync` plugin and `convexJwt` for JWT integration, highlighting essential environment variables for a secure setup.

import { betterAuth } from "better-auth";
import { syncPlugin } from "better-auth-sync";
import { convexJwt } from "better-auth-sync/jwt";

// Ensure these environment variables are set in your Better Auth server environment
// BETTER_AUTH_URL=https://auth.your-app.com
// WEBHOOK_URL=https://your-project.convex.site/auth-webhook
// WEBHOOK_SECRET=replace-with-a-long-random-secret
// APP_ORIGIN=https://your-app.com

export const auth = betterAuth({
  // Replace with your actual adapter, trusted origins, and providers
  // adapter: ..., 
  // trustedOrigins: [process.env.APP_ORIGIN!],
  // providers: { email: { ... } },
  plugins: [
    convexJwt({
      issuer: process.env.APP_ORIGIN ?? 'https://your-app.com',
      audience: process.env.APP_ORIGIN ?? 'https://your-app.com'
    }),
    syncPlugin({
      secret: process.env.WEBHOOK_SECRET ?? 'your-webhook-secret-placeholder',
      url: process.env.WEBHOOK_URL ?? 'https://your-project.convex.site/auth-webhook',
      retryAttempts: 3
    })
  ]
});

console.log('Better Auth instance configured with sync and Convex JWT plugins.');
console.log('Ensure environment variables are correctly loaded and JWT issuer/audience match APP_ORIGIN.');

view raw JSON →