
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@polar-sh/better-auth
Advanced tools
A Better Auth plugin for integrating Polar payments and subscriptions into your authentication flow.
pnpm add better-auth @polar-sh/better-auth @polar-sh/sdk
Go to your Polar Organization Settings, and create an Organization Access Token. Add it to your environment.
# .env
POLAR_ACCESS_TOKEN=...
import { betterAuth } from "better-auth";
import { polar } from "@polar-sh/better-auth";
import { Polar } from "@polar-sh/sdk";
const client = new Polar({
accessToken: process.env.POLAR_ACCESS_TOKEN,
// Use 'sandbox' if you're using the Polar Sandbox environment
// Remember that access tokens, products, etc. are completely separated between environments.
// Access tokens obtained in Production are for instance not usable in the Sandbox environment.
server: 'production'
});
const auth = betterAuth({
// ... Better Auth config
plugins: [
polar({
client,
// Enable automatic Polar Customer creation on signup
createCustomerOnSignUp: true,
// Enable customer portal
enableCustomerPortal: true, // Deployed under /portal for authenticated users
// Configure checkout
checkout: {
enabled: true,
products: [
{
productId: "123-456-789", // ID of Product from Polar Dashboard
slug: "pro" // Custom slug for easy reference in Checkout URL, e.g. /checkout/pro
}
],
successUrl: "/success?checkout_id={CHECKOUT_ID}"
},
// Incoming Webhooks handler will be installed at /polar/webhooks
webhooks: {
secret: process.env.POLAR_WEBHOOK_SECRET,
onPayload: ...,
}
})
]
});
client
: Polar SDK client instancecreateCustomerOnSignUp
: Automatically create a Polar customer when a user signs upgetCustomerCreateParams
: Custom function to provide additional customer creation parametersenableCustomerPortal
: Enable the customer portal functionality. Deployed as GET endpoint at /portalcheckout.enabled
: Enable checkout functionality. Deployed as GET endpoint at /checkoutcheckout.products
: Array of products or function returning products. Slug passed will then be passable as route param.checkout.successUrl
: URL to redirect to after successful checkoutConfigure a Webhook endpoint in your Polar Organization Settings page. Webhook endpoint is configured at /polar/webhooks.
Add the secret to your environment.
# .env
POLAR_WEBHOOK_SECRET=...
The plugin supports handlers for all Polar webhook events:
onPayload
- Catch-all handler for any incoming Webhook eventonCheckoutCreated
- Triggered when a checkout is createdonCheckoutUpdated
- Triggered when a checkout is updatedonOrderCreated
- Triggered when an order is createdonOrderRefunded
- Triggered when an order is refundedonRefundCreated
- Triggered when a refund is createdonRefundUpdated
- Triggered when a refund is updatedonSubscriptionCreated
- Triggered when a subscription is createdonSubscriptionUpdated
- Triggered when a subscription is updatedonSubscriptionActive
- Triggered when a subscription becomes activeonSubscriptionCanceled
- Triggered when a subscription is canceledonSubscriptionRevoked
- Triggered when a subscription is revokedonSubscriptionUncanceled
- Triggered when a subscription cancellation is reversedonProductCreated
- Triggered when a product is createdonProductUpdated
- Triggered when a product is updatedonOrganizationUpdated
- Triggered when an organization is updatedonBenefitCreated
- Triggered when a benefit is createdonBenefitUpdated
- Triggered when a benefit is updatedonBenefitGrantCreated
- Triggered when a benefit grant is createdonBenefitGrantUpdated
- Triggered when a benefit grant is updatedonBenefitGrantRevoked
- Triggered when a benefit grant is revokedonCustomerCreated
- Triggered when a customer is createdonCustomerUpdated
- Triggered when a customer is updatedonCustomerDeleted
- Triggered when a customer is deletedonCustomerStateChanged
- Triggered when a customer is createdThe plugin adds the following API routes:
GET /checkout/:slug
- Redirect to Polar checkoutGET /state
- Customer state (Customer Data, Active Subscriptions, Entitlements, etc.) for the authenticated userGET /customer-portal
- Redirects to Polar Customer Portal for authenticated userPOST /polar/webhooks
- Incoming webhooks are automatically parsed & validatedWhen createCustomerOnSignUp
is enabled, a new Polar Customer is automatically created when a new User is added in the Better-Auth Database.
All new customers are created with an associated externalId
, which is the ID of your User in the Database. This allows us to skip any Polar <-> User mapping in your Database.
When checkouts are enabled, you're able to initialize Checkout Sessions on the /checkout/:slug
route.
If you pass an array of products to the configuration, you're able to use the slug as a reference instead of using the product id.
All your organization products are eligible for checkouts, even if they're not passed to the products-configuration.
You can initialize a checkout session like following - /checkout?productId=123-456-789
FAQs
Polar integration for better-auth
The npm package @polar-sh/better-auth receives a total of 378 weekly downloads. As such, @polar-sh/better-auth popularity was classified as not popular.
We found that @polar-sh/better-auth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.