affiliate-better-auth
v0.6.6
Published
A tiny affiliate/referral plugin for Better Auth.
Maintainers
Readme
affiliate-better-auth
A tiny affiliate/referral plugin for Better Auth.
Install
pnpm add affiliate-better-auth better-auth
# or
npm i affiliate-better-auth better-auth
# or
yarn add affiliate-better-auth better-authServer usage
import { betterAuth } from "better-auth";
import { affiliatePlugin } from "affiliate-better-auth";
export const auth = betterAuth({
plugins: [affiliatePlugin()],
});- Adds endpoints and hooks to handle simple referral tracking.
- Automatically creates minimal schema for
affiliate_codeandaffiliatemodels via Better Auth adapter.
Client usage
import { createAuthClient } from "better-auth/client";
import { affiliateClientPlugin } from "affiliate-better-auth";
export const auth = createAuthClient({
plugins: [affiliateClientPlugin()],
});Endpoints
GET /affiliate/generate-link- Requires an authenticated session
- Returns
{ link: string }wherelinkis the user’s referral code - Example: build a URL like
https://yourapp.com/?ref=<code>
POST /affiliate/issue-cookie- Body:
{ code: string } - Sets an
affiliatecookie with the provided code
- Body:
When a new user is created while an affiliate cookie is present, the plugin records a referral entry using the configured Better Auth adapter.
Notes
- Peer dependency:
better-auth(see package.json for supported versions) - Cookie name:
affiliate - Codes are short random strings generated server-side.
