Skip to content

Open source CapCut alternative.

TypeScript

feat(site): grant signup bonuses to new users

David Leb67cc82cdb23
diff --git a/site/src/lib/auth.ts b/site/src/lib/auth.ts
index 37f04a63..a7922e49 100644
--- a/site/src/lib/auth.ts
+++ b/site/src/lib/auth.ts
@@ -3,6 +3,7 @@ import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { oneTimeToken } from "better-auth/plugins";
+import { provisionSignupGrants } from "@/lib/onboarding/signup-grants";
import { prisma } from "@/lib/prisma";
// Prefix for issued Vision API keys. The full secret is shown to the developer
@@ -27,6 +28,18 @@ export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
+ databaseHooks: {
+ user: {
+ create: {
+ // Every new account is provisioned with its signup grants (app credits
+ // + free Vision API calls). provisionSignupGrants is idempotent and
+ // swallows its own errors, so it never blocks user creation.
+ after: async (user) => {
+ await provisionSignupGrants(user.id);
+ },
+ },
+ },
+ },
emailAndPassword: {
enabled: false,
},
diff --git a/site/src/lib/onboarding/signup-grants.ts b/site/src/lib/onboarding/signup-grants.ts
new file mode 100644
index 00000000..91a0f369
--- /dev/null
+++ b/site/src/lib/onboarding/signup-grants.ts
@@ -0,0 +1,52 @@
+import { creditStringToMicros } from "@/lib/credits/amounts";
+import { grantCredits } from "@/lib/credits/inference";
+import { grantVisionCalls } from "@/lib/credits/vision-grants";
+
+// Single source of truth for what a new account starts with. Both grants are
+// idempotent and keyed to the user, so provisioning can run more than once (e.g.
+// a retried signup) without ever double-granting.
+export const signupAppCredits = "1"; // USD added to the app inference balance
+export const signupVisionFreeCalls = 100; // lifetime free Vision API calls
+
+export async function provisionSignupGrants(userId: string): Promise<void> {
+ // Settle the two grants independently: one failing must not block the other,
+ // and signup itself must never fail because a bonus grant hiccupped.
+ const results = await Promise.allSettled([
+ grantSignupAppCredits(userId),
+ grantSignupVisionCalls(userId),
+ ]);
+
+ for (const result of results) {
+ if (result.status === "rejected") {
+ console.error("[signup-grants] failed to provision a signup grant", {
+ reason: result.reason,
+ userId,
+ });
+ }
+ }
+}
+
+async function grantSignupAppCredits(userId: string): Promise<void> {
+ // grantCredits dedupes on (source, sourceId, userId), so this is a no-op on
+ // re-run.
+ await grantCredits({
+ amountMicros: creditStringToMicros(signupAppCredits),
+ description: "Signup bonus credits",
+ source: "signup",
+ sourceId: `signup-app-credit:${userId}`,
+ userId,
+ });
+}
+
+async function grantSignupVisionCalls(userId: string): Promise<void> {
+ // A one-time vision_call grant — no subscription required. The Vision route
+ // and the api-keys gate both accept grant balance as access. Idempotent via
+ // (userId, unit, source, sourceId).
+ await grantVisionCalls({
+ calls: signupVisionFreeCalls,
+ description: "Signup bonus Vision API calls",
+ source: "signup",
+ sourceId: `signup-vision-calls:${userId}`,
+ userId,
+ });
+}