Affiliate Better Auth
affiliate-better-auth is a specialized plugin designed to integrate referral and affiliate tracking capabilities into applications built with the Better Auth authentication system. It currently stands at version 0.6.6, with recent releases indicating an active development cadence, though the changelog mentions `0.1.0` for multiple versions. The library simplifies the process of adding referral links and tracking by automatically creating necessary database schemas via the Better Auth adapter. It provides both server-side and client-side plugins to manage referral code generation, cookie issuance for tracking, and recording referrals upon new user creation. Its key differentiators include its tight integration with the Better Auth ecosystem, offering a streamlined solution for applications already using Better Auth for authentication, rather than a standalone, more complex affiliate platform.
Common errors
-
Error: Cannot find module 'affiliate-better-auth' or 'better-auth'
cause One or both packages are not installed or are not resolvable in the module path. This can happen with incorrect `pnpm`, `npm`, or `yarn` commands.fixRun `pnpm add affiliate-better-auth better-auth` (or `npm i` / `yarn add`) in your project root to ensure both the plugin and its peer dependency are correctly installed. -
TypeError: auth.plugins.affiliate.generateLink is not a function
cause The `affiliatePlugin` was not correctly initialized or the `auth` instance does not have the expected `affiliate` property on its `plugins` object.fixEnsure `affiliatePlugin()` is included in the `plugins` array when configuring `betterAuth` on the server: `betterAuth({ plugins: [affiliatePlugin()] })`. -
Error: Peer dependency 'better-auth@^1.3.0' not installed or incompatible.
cause Your installed version of `better-auth` does not match the required peer dependency range for `affiliate-better-auth`.fixInstall a compatible version of `better-auth`. For example, `npm install better-auth@^1.3.0` or update your `package.json` and run `npm install`.
Warnings
- breaking This package is tightly coupled with `better-auth`. Major version upgrades of `better-auth` (e.g., from 1.x to 2.x) may introduce breaking changes requiring updates to `affiliate-better-auth` or its configuration.
- gotcha Referral tracking relies on an `affiliate` cookie being set. Users blocking cookies or using incognito modes may not be accurately tracked, or the referral link logic might not function as expected without manual handling.
- gotcha The package automatically creates minimal schema for `affiliate_code` and `affiliate` models via the Better Auth adapter. If you have existing tables or a different schema preference, this might lead to conflicts or require manual migration/adjustments.
- gotcha While recent `affiliate-better-auth` versions show `0.6.x`, the provided changelog consistently refers to `0.1.0`. This discrepancy might indicate a versioning inconsistency or a misaligned changelog, which can make tracking specific changes or breaking points difficult.
Install
-
npm install affiliate-better-auth -
yarn add affiliate-better-auth -
pnpm add affiliate-better-auth
Imports
- affiliatePlugin
const { affiliatePlugin } = require('affiliate-better-auth');import { affiliatePlugin } from 'affiliate-better-auth'; - affiliateClientPlugin
const { affiliateClientPlugin } = require('affiliate-better-auth');import { affiliateClientPlugin } from 'affiliate-better-auth'; - betterAuth
import betterAuth from 'better-auth';
import { betterAuth } from 'better-auth'; - createAuthClient
import { createAuthClient } from 'better-auth';import { createAuthClient } from 'better-auth/client';
Quickstart
import { betterAuth } from 'better-auth';
import { affiliatePlugin } from 'affiliate-better-auth';
// Configure Better Auth with the affiliate plugin
export const auth = betterAuth({
plugins: [
// Initialize the affiliate plugin on the server
affiliatePlugin()
]
});
// Example of accessing an affiliate endpoint (conceptual, not runnable directly without a server setup)
// (You would typically expose these via your API routes)
// auth.get('/affiliate/generate-link', (req, res) => {
// if (!req.session) return res.status(401).send('Unauthorized');
// const link = auth.plugins.affiliate.generateLink(req.session.userId);
// res.json({ link });
// });
// Client-side example (requires a separate client-side bundle)
// import { createAuthClient } from 'better-auth/client';
// import { affiliateClientPlugin } from 'affiliate-better-auth';
// export const clientAuth = createAuthClient({
// plugins: [
// affiliateClientPlugin()
// ]
// });
// // To issue a cookie from the client after receiving a referral code, for example from a URL query parameter
// // clientAuth.plugins.affiliate.issueCookie(codeFromQueryParams);