Affiliate Better Auth

0.6.6 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates server-side initialization of `affiliate-better-auth` by integrating `affiliatePlugin` into the `betterAuth` instance, enabling referral tracking functionalities and automatic schema generation.

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);

view raw JSON →