From cf-saas-stack
Better Auth authentication patterns and conventions for this Cloudflare Workers project
How this skill is triggered — by the user, by Claude, or both
Slash command
/cf-saas-stack:authThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
- This project uses **Better Auth** (`better-auth` v1.3.34+) for authentication
better-auth v1.3.34+) for authentication/api/authcreateAuth() from @/auth/server to create auth instancesdatabase: D1Database instance from Cloudflare contextoptions: Object containing:
secret: BETTER_AUTH_SECRET from environment variablesgoogleClientId: GOOGLE_CLIENT_ID from environment variablesgoogleClientSecret: GOOGLE_CLIENT_SECRET from environment variablesimport { createAuth } from "@/auth/server";
const auth = await createAuth(context.cloudflare.env.DATABASE, {
secret: context.cloudflare.env.BETTER_AUTH_SECRET,
googleClientId: context.cloudflare.env.GOOGLE_CLIENT_ID,
googleClientSecret: context.cloudflare.env.GOOGLE_CLIENT_SECRET,
});
/api/auth via app/routes/api/auth.$.tsloader and action should call auth.handler(request)auth.api.getSession({ headers }) to get current session{ session, user } or null if not authenticatedauthClient from @/auth/client for client-side operations/api/auth and admin plugin enabledauthClient.signIn.email() for login, authClient.signUp.email() for signupauthClient.signOut() for logoutauthClient.useSession() hook for React componentscreateTRPCContextauthApi (Better Auth API) and auth (session data)protectedProcedure for authenticated-only endpoints (guarantees ctx.auth.user and ctx.auth.session)adminProcedure for admin-only endpoints (checks user.role === "admin")@/db/schema, database connection via getDb() from @/dbBETTER_AUTH_SECRET in client-side codeprotectedProcedure or adminProcedure for sensitive operationsUNAUTHORIZED (401) and FORBIDDEN (403) errorsUNAUTHORIZED and FORBIDDENconst auth = await createAuth(env.DATABASE, env.BETTER_AUTH_SECRET);
const session = await auth.api.getSession({ headers: request.headers });
if (!session) {
throw redirect("/login");
}
import { authClient } from "@/auth/client";
const { data: session } = authClient.useSession();
npx claudepluginhub casper-studios/casper-marketplace --plugin cf-saas-stackIntegrates Better Auth authentication framework for Cloudflare D1, Next.js, Nuxt, and 15+ frameworks. Helps with D1 adapter errors, OAuth, 2FA, RBAC.
Better Auth in TypeScript — setting up the auth instance, picking adapters, wiring framework route handlers, configuring sessions and cookies, adding plugins (2FA, organization, admin, magicLink, JWT), or porting from NextAuth/Auth.js, Clerk, Auth0, or Supabase Auth. Covers Next.js, SvelteKit, Hono, Express, Nuxt, Astro, and React/Vue/Svelte clients. Trigger when writing, reviewing, or migrating Better Auth code — and even when the user doesn't explicitly mention Better Auth but is working on TypeScript authentication, session cookies, OAuth providers, or auth-library migration. Contains 42 rules organized by impact across 8 categories.
Read sessions client- and server-side, guard routes, and run sign in/up/out with Better Auth. Use when gating pages/APIs on auth or wiring auth flows.