Open source CapCut alternative.
TypeScript
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import { bearer } from "better-auth/plugins";
import { AUTH_COOKIE_DOMAIN, AUTH_SUBDOMAINS, DONKEYCUT_CANONICAL } from "@/cut/lib/hosts";
import { trackSignup } from "@/lib/meta/signup";
import { provisionSignupGrants } from "@/lib/onboarding/signup-grants";
import { prisma } from "@/lib/prisma";
import { emailPasswordDisabledPaths, emailPasswordGuard, emailPasswordOptions } from "@/lib/email-password-auth";
import { AUTH_ERROR_PATH } from "@/lib/signInReturn";
// donkeycut.com owns sign-in: the auth pages, the auth API, and the Google
// OAuth callback all serve on that one origin (the proxy 308s www. to the apex
// before anything serves). Hosted deploys pin baseURL there — it decides the
// OAuth redirect_uri — while local dev leaves it unset and better-auth derives
// it from the localhost request.
const baseURL = process.env.VERCEL ? DONKEYCUT_CANONICAL : undefined;
// The session is shared with the apex's subdomains, which sign in through
// this host; a host-only cookie would never reach them. Scoping the auth
// cookies to the registrable host lets the session ride across, and trusting
// the subdomains lets a sign-in started on one name its own address as the
// post-auth callback. Hosted only —
// localhost can't carry a Domain attribute.
const crossSubDomain = process.env.VERCEL
? {
advanced: {
crossSubDomainCookies: { domain: AUTH_COOKIE_DOMAIN, enabled: true },
},
trustedOrigins: [DONKEYCUT_CANONICAL, AUTH_SUBDOMAINS],
}
: {};
// The iOS app signs in natively and exchanges the provider's ID token for a
// session at /api/auth/sign-in/social. ID tokens are verified against these
// native OAuth clients alongside the web client. The Google iOS client id is
// deployment-specific, so it rides the environment (GOOGLE_IOS_CLIENT_ID)
// with the other Google OAuth values.
const IOS_APP_BUNDLE_ID = "com.donkeycut.donkeycut";
const APPLE_SERVICES_ID = "com.donkeycut.signin";
export const auth = betterAuth({
...crossSubDomain,
baseURL,
secret: process.env.BETTER_AUTH_SECRET,
// Sessions last a year, and the rolling expiry is refreshed daily on use, so an active user
// effectively never has to sign in again.
//
// The signed cookie carries the session and its user for five minutes, so the
// editor's polling surfaces — the exports dock, the storage meter, the credit
// pill — stop paying a session read and a user read for every request they
// make. A revoked session or a changed flag takes up to that long to bite.
session: {
cookieCache: { enabled: true, maxAge: 5 * 60 },
expiresIn: 60 * 60 * 24 * 365,
updateAge: 60 * 60 * 24,
},
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
databaseHooks: {
user: {
create: {
// Every new account is provisioned with its signup credit grant, its
// starter project, and its fonts folder. provisionSignupGrants is
// idempotent and swallows its own errors, so it never blocks user
// creation. The request's headers carry the network the signup came
// from.
after: async (user, context) => {
const headers = context?.headers ?? context?.request?.headers;
await provisionSignupGrants(
{
email: user.email,
id: user.id,
},
headers,
);
trackSignup(user, headers);
},
},
},
},
emailAndPassword: emailPasswordOptions,
disabledPaths: emailPasswordDisabledPaths,
// A provider callback that fails comes back to sign-in, which says so and
// offers the button again; left unset, production sends it to the landing
// page with only an `?error=` code to show for it.
onAPIError: { errorURL: AUTH_ERROR_PATH },
hooks: {
before: emailPasswordGuard,
},
socialProviders: {
google: {
// The first entry drives the web OAuth flow; the rest are accepted
// audiences for native ID-token sign-in.
clientId: [
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_IOS_CLIENT_ID,
].filter((id): id is string => !!id),
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
// Always show Google's account chooser: without this, a browser with one
// signed-in Google session silently reuses the last-used account.
prompt: "select_account",
},
apple: {
clientId: APPLE_SERVICES_ID,
clientSecret: process.env.APPLE_CLIENT_SECRET ?? "",
// Native Sign in with Apple mints tokens whose audience is the app's
// bundle id.
appBundleIdentifier: IOS_APP_BUNDLE_ID,
},
},
// The iOS app holds its session as a bearer token (`set-auth-token`
// response header, then `Authorization: Bearer` on every call).
//
// nextCookies writes better-auth's own cookies through Next's cookie helper,
// so a session read inside one of our route handlers can refresh the cached
// session cookie. Without it the cache would only ever be written by the auth
// routes and would lapse five minutes after a page load. It goes last: the
// hook has to see what every plugin before it wants to set.
plugins: [bearer(), nextCookies()],
});