😽
【NextJs + BetterAuth + Polar】Subcription サブスクサービスの実装【#9 Polar Setup】
【#9 Polar Setup】
YouTube: https://youtu.be/n7pUrHbuRIc
今回はBetterAuthとPolarを連携を実装していきます。
今回の動画では各ライブラリは
以下のバージョンを指定して使用しています。
"better-auth": "^1.3.26",
"@polar-sh/better-auth": "^1.1.9",
"@polar-sh/sdk": "^0.35.4",
.env
DATABASE_URL="postgresql://neondb_owner:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
BETTER_AUTH_SECRET="Ahl8Nnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
BETTER_AUTH_URL="http://localhost:3000"
POLAR_ACCESS_TOKEN=""
lib/polar-client.ts
import { Polar } from "@polar-sh/sdk";
export const polarClient = new Polar({
accessToken: process.env.POLAR_ACCESS_TOKEN,
server: "sandbox",
});
lib/auth.ts
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { polar, checkout, portal } from "@polar-sh/better-auth";
import { db } from "@/src"; // your drizzle instance
import * as schema from "@/src/db/schema";
import { polarClient } from "./polar-client";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
schema: {
...schema,
},
}),
emailAndPassword: {
enabled: true,
},
plugins: [
polar({
client: polarClient,
createCustomerOnSignUp: true,
use: [
checkout({
products: [
{
productId: "",
slug: "",
},
],
successUrl: "/success?checkout_id={CHECKOUT_ID}",
authenticatedUsersOnly: true,
}),
portal(),
],
}),
],
});
lib/auth-client.ts
import { createAuthClient } from "better-auth/react";
import { polarClient } from "@polar-sh/better-auth";
export const authClient = createAuthClient({
plugins: [polarClient()],
});
Discussion