Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.
Python aianthropicautomationclaude-codeclaude-skillscloudflaredevtoolsproductivityreactskillstailwindvite
skill/better-auth: Add Issue 13 - Nanostore session invalidation pattern (v2.2.1)
Jezb67bf8a87929
diff --git a/skills/better-auth/.claude-plugin/plugin.json b/skills/better-auth/.claude-plugin/plugin.jsonindex 98d87e85..9c44b30f 100644--- a/skills/better-auth/.claude-plugin/plugin.json+++ b/skills/better-auth/.claude-plugin/plugin.json@@ -1,12 +1,12 @@{"name": "better-auth",- "description": "Production-ready authentication framework for TypeScript with Cloudflare D1 support via Drizzle ORM or Kysely. Use this skill when building auth systems as a self-hosted alternative to Clerk or Auth.js, particularly for Cloudflare Workers projects. CRITICAL: better-auth requires Drizzle ORM or Kysely as database adapters - there is NO direct D1 adapter. Supports social providers (Google, GitHub, Microsoft, Apple), email/password, magic links, 2FA, passkeys, organizations, and RBAC. Prevents 12+ ",- "version": "1.0.0",+ "description": "Production-ready authentication framework for TypeScript with Cloudflare D1 support via Drizzle ORM or Kysely. Self-hosted alternative to Clerk or Auth.js for Cloudflare Workers projects. CRITICAL: better-auth requires Drizzle ORM or Kysely - NO direct D1 adapter exists. Supports social providers, 2FA, passkeys, organizations, RBAC. Prevents 13 common auth errors including D1 adapter misconfiguration, nanostore session invalidation with TanStack Query, schema generation, CORS, OAuth flows, and JWT handling.",+ "version": "2.2.1","author": {"name": "Jeremy Dawes","email": "[email protected]"},"license": "MIT","repository": "https://github.com/jezweb/claude-skills",- "keywords": ["better-auth","authentication","cloudflare d1 auth","drizzle orm auth","kysely auth","self-hosted auth","typescript auth","clerk alternative","auth.js alternative","social login","oauth providers","session management","jwt tokens","2fa","two-factor"]+ "keywords": ["better-auth","authentication","cloudflare-d1","drizzle-orm","kysely","self-hosted-auth","typescript-auth","clerk-alternative","authjs-alternative","social-auth","oauth","session-management","jwt","2fa","passkeys","tanstack-query","react-query","nanostores","session-invalidation"]}diff --git a/skills/better-auth/README.md b/skills/better-auth/README.mdindex 3c7b2f15..562aa2ef 100644--- a/skills/better-auth/README.md+++ b/skills/better-auth/README.md@@ -38,6 +38,8 @@ This skill should be automatically invoked when you mention:- **"multi-session"** - Account switching- **"genericOAuth"** - Custom OAuth providers- **"API key authentication"** - API-only auth+- **"TanStack Query session"** - Session state with React Query+- **"nanostores auth"** - Nanostore session invalidation---@@ -74,24 +76,21 @@ This skill should be automatically invoked when you mention:8. **Database Setup** - D1 and PostgreSQL schema patterns9. **API Reference** - Complete documentation for 80+ auto-generated endpoints-### Errors Prevented (16 Common Issues)+### Errors Prevented (13 Common Issues)- ✅ **D1 adapter misconfiguration** (no direct d1Adapter, must use Drizzle/Kysely)- ✅ **Schema generation failures** (using Drizzle Kit correctly)- ✅ **TanStack Start cookie issues** (reactStartCookies plugin required)- ✅ **Plugin ordering errors** (reactStartCookies must be last)+- ✅ **Nanostore session invalidation** (TanStack Query won't refresh session state)- ✅ D1 eventual consistency causing stale session reads- ✅ CORS misconfiguration for SPA applications- ✅ Session serialization errors in Workers- ✅ OAuth redirect URI mismatch- ✅ Email verification not sending- ✅ JWT token expiration issues-- ✅ Password hashing performance bottlenecks- ✅ Social provider scope issues (missing user data)-- ✅ Multi-tenant data leakage-- ✅ Rate limit false positives-- ✅ Using wrong API approach (endpoints vs server methods)-- ✅ Reinventing existing auto-generated endpoints+- ✅ TypeScript errors with Drizzle schema### Reference Filesdiff --git a/skills/better-auth/SKILL.md b/skills/better-auth/SKILL.mdindex 3c518062..e580f2e6 100644--- a/skills/better-auth/SKILL.md+++ b/skills/better-auth/SKILL.md@@ -1,17 +1,17 @@---name: better-authdescription: |- Production-ready authentication framework for TypeScript with Cloudflare D1 support via Drizzle ORM or Kysely. Use this skill when building auth systems as a self-hosted alternative to Clerk or Auth.js, particularly for Cloudflare Workers projects. CRITICAL: better-auth requires Drizzle ORM or Kysely as database adapters - there is NO direct D1 adapter. Supports social providers (Google, GitHub, Microsoft, Apple), email/password, magic links, 2FA, passkeys, organizations, and RBAC. Includes comprehensive API reference for 80+ auto-generated endpoints, server-side methods, and complete plugin documentation (multiSession, genericOAuth, apiKey, TanStack Start integration). Prevents 14+ common authentication errors including D1 adapter misconfiguration, schema generation issues, session serialization, CORS, OAuth flows, JWT token handling, and API usage patterns.+ Production-ready authentication framework for TypeScript with Cloudflare D1 support via Drizzle ORM or Kysely. Use this skill when building auth systems as a self-hosted alternative to Clerk or Auth.js, particularly for Cloudflare Workers projects. CRITICAL: better-auth requires Drizzle ORM or Kysely as database adapters - there is NO direct D1 adapter. Supports social providers (Google, GitHub, Microsoft, Apple), email/password, magic links, 2FA, passkeys, organizations, and RBAC. Includes comprehensive API reference for 80+ auto-generated endpoints, server-side methods, and complete plugin documentation (multiSession, genericOAuth, apiKey, TanStack Start integration). Prevents 13 common authentication errors including D1 adapter misconfiguration, schema generation issues, session serialization, CORS, OAuth flows, JWT token handling, API usage patterns, and nanostore state invalidation.- Keywords: better-auth, authentication, cloudflare d1 auth, drizzle orm auth, kysely auth, self-hosted auth, typescript auth, clerk alternative, auth.js alternative, social login, oauth providers, session management, jwt tokens, 2fa, two-factor, passkeys, webauthn, multi-tenant auth, organizations, teams, rbac, role-based access, google auth, github auth, microsoft auth, apple auth, magic links, email password, better-auth setup, drizzle d1, kysely d1, session serialization error, cors auth, d1 adapter, better-auth endpoints, better-auth api, auth.api methods, auto-generated endpoints, server-side api, tanstack start, reactStartCookies, multiSession, multi-session, genericOAuth, custom oauth, apiKey, api-key auth+ Keywords: better-auth, authentication, cloudflare d1 auth, drizzle orm auth, kysely auth, self-hosted auth, typescript auth, clerk alternative, auth.js alternative, social login, oauth providers, session management, jwt tokens, 2fa, two-factor, passkeys, webauthn, multi-tenant auth, organizations, teams, rbac, role-based access, google auth, github auth, microsoft auth, apple auth, magic links, email password, better-auth setup, drizzle d1, kysely d1, session serialization error, cors auth, d1 adapter, better-auth endpoints, better-auth api, auth.api methods, auto-generated endpoints, server-side api, tanstack start, reactStartCookies, multiSession, multi-session, genericOAuth, custom oauth, apiKey, api-key auth, tanstack query, react query, nanostores, session invalidation, stale sessionlicense: MITmetadata:- version: 2.2.0+ version: 2.2.1last_verified: 2025-11-18production_tested: multiple (zpg6/better-auth-cloudflare, zwily/example-react-router-cloudflare-d1-drizzle-better-auth, foxlau/react-router-v7-better-auth, matthewlynch/better-auth-react-router-cloudflare-d1)package_version: 1.3.34token_savings: ~75%- errors_prevented: 16+ errors_prevented: 13official_docs: https://better-auth.comgithub: https://github.com/better-auth/better-authbreaking_changes: v2.0.0 - Corrected D1 adapter patterns (Drizzle/Kysely required)@@ -1743,6 +1743,51 @@ wrangler dev---+### Issue 13: User Data Updates Not Reflecting in UI (with TanStack Query)++**Problem**: After updating user data (e.g., avatar, name), changes don't appear in `useSession()` despite calling `queryClient.invalidateQueries()`.++**Symptoms**: Avatar image or user profile data appears stale after successful update. TanStack Query cache shows updated data, but better-auth session still shows old values.++**Root Cause**: better-auth uses **nanostores** for session state management, not TanStack Query. Calling `queryClient.invalidateQueries()` only invalidates React Query cache, not the better-auth nanostore.++**Solution**: Manually notify the nanostore after updating user data:++```typescript+// Update user data+const { data, error } = await authClient.updateUser({+ image: newAvatarUrl,+ name: newName+})++if (!error) {+ // Manually invalidate better-auth session state+ authClient.$store.notify('$sessionSignal')++ // Optional: Also invalidate React Query if using it for other data+ queryClient.invalidateQueries({ queryKey: ['user-profile'] })+}+```++**When to use**:+- Using better-auth + TanStack Query together+- Updating user profile fields (name, image, email)+- Any operation that modifies session user data client-side++**Alternative**: Call `refetch()` from `useSession()`, but `$store.notify()` is more direct:++```typescript+const { data: session, refetch } = authClient.useSession()+// After update+await refetch()+```++**Note**: `$store` is an undocumented internal API. This pattern is production-validated but may change in future better-auth versions.++**Source**: Community-discovered pattern, production use verified++---+## Comparison: better-auth vs Alternatives| Feature | better-auth | Clerk | Auth.js |