Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Set up Convex authentication with Convex Auth, Clerk, WorkOS AuthKit, Auth0, or custom JWT providers.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: convex-setup-auth3description:4Sets up Convex auth, identity mapping, and access control. Use for login, auth5providers, users tables, protected functions, or roles in a Convex app.6---78# Convex Authentication Setup910Implement secure authentication in Convex with user management and access11control.1213## When to Use1415- Setting up authentication for the first time16- Implementing user management (users table, identity mapping)17- Creating authentication helper functions18- Setting up auth providers (Convex Auth, Clerk, WorkOS AuthKit, Auth0, custom19JWT)2021## When Not to Use2223- Auth for a non-Convex backend24- Pure OAuth/OIDC documentation without a Convex implementation25- Debugging unrelated bugs that happen to surface near auth code26- The auth provider is already fully configured and the user only needs a27one-line fix2829## First Step: Choose the Auth Provider3031Convex supports multiple authentication approaches. Do not assume a provider.3233Before writing setup code:34351. Ask the user which auth solution they want, unless the repository already36makes it obvious372. If the repo already uses a provider, continue with that provider unless the38user wants to switch393. If the user has not chosen a provider and the repo does not make it obvious,40ask before proceeding4142Common options:4344- [Convex Auth](https://docs.convex.dev/auth/convex-auth) - good default when45the user wants auth handled directly in Convex46- [Clerk](https://docs.convex.dev/auth/clerk) - use when the app already uses47Clerk or the user wants Clerk's hosted auth features48- [WorkOS AuthKit](https://docs.convex.dev/auth/authkit/) - use when the app49already uses WorkOS or the user wants AuthKit specifically50- [Auth0](https://docs.convex.dev/auth/auth0) - use when the app already uses51Auth052- Custom JWT provider - use when integrating an existing auth system not covered53above5455Look for signals in the repo before asking:5657- Dependencies such as `@clerk/*`, `@workos-inc/*`, `@auth0/*`, or Convex Auth58packages59- Existing files such as `convex/auth.config.ts`, auth middleware, provider60wrappers, or login components61- Environment variables that clearly point at a provider6263## After Choosing a Provider6465Read the provider's official guide and the matching local reference file:6667- Convex Auth: [official docs](https://docs.convex.dev/auth/convex-auth), then68`references/convex-auth.md`69- Clerk: [official docs](https://docs.convex.dev/auth/clerk), then70`references/clerk.md`71- WorkOS AuthKit: [official docs](https://docs.convex.dev/auth/authkit/), then72`references/workos-authkit.md`73- Auth0: [official docs](https://docs.convex.dev/auth/auth0), then74`references/auth0.md`7576The local reference files contain the concrete workflow, expected files and env77vars, gotchas, and validation checks.7879Use those sources for:8081- package installation82- client provider wiring83- environment variables84- `convex/auth.config.ts` setup85- login and logout UI patterns86- framework-specific setup for React, Vite, or Next.js8788For shared auth behavior, use the official Convex docs as the source of truth:8990- [Auth in Functions](https://docs.convex.dev/auth/functions-auth) for91`ctx.auth.getUserIdentity()`92- [Storing Users in the Convex Database](https://docs.convex.dev/auth/database-auth)93for optional app-level user storage94- [Authentication](https://docs.convex.dev/auth) for general auth and95authorization guidance96- [Convex Auth Authorization](https://labs.convex.dev/auth/authz) when the97provider is Convex Auth9899Prefer official docs over recalled steps, because provider CLIs and Convex Auth100internals change between versions. Inventing setup from memory risks outdated101patterns. For third-party providers, only add app-level user storage if the app102actually needs user documents in Convex. Not every app needs a `users` table.103For Convex Auth, follow the Convex Auth docs and built-in auth tables rather104than adding a parallel `users` table plus `storeUser` flow, because Convex Auth105already manages user records internally. After running provider initialization106commands, verify generated files and complete the post-init wiring steps the107provider reference calls out. Initialization commands rarely finish the entire108integration.109110## Core Pattern: Protecting Backend Functions111112The most common auth task is checking identity in Convex functions.113114```ts115// Bad: trusting a client-provided userId116export const getMyProfile = query({117args: { userId: v.id("users") },118handler: async (ctx, args) => {119return await ctx.db.get(args.userId);120},121});122```123124```ts125// Good: verifying identity server-side126export const getMyProfile = query({127args: {},128handler: async (ctx) => {129const identity = await ctx.auth.getUserIdentity();130if (!identity) throw new Error("Not authenticated");131132return await ctx.db133.query("users")134.withIndex("by_tokenIdentifier", (q) =>135q.eq("tokenIdentifier", identity.tokenIdentifier),136)137.unique();138},139});140```141142## Workflow1431441. Determine the provider, either by asking the user or inferring from the repo1452. Ask whether the user wants local-only setup or production-ready setup now1463. Read the matching provider reference file1474. Follow the official provider docs for current setup details1485. Follow the official Convex docs for shared backend auth behavior, user149storage, and authorization patterns1506. Only add app-level user storage if the docs and app requirements call for it1517. Add authorization checks for ownership, roles, or team access only where the152app needs them1538. Verify login state, protected queries, environment variables, and production154configuration if requested155156If the flow blocks on interactive provider or deployment setup, ask the user157explicitly for the exact human step needed, then continue after they complete158it. For UI-facing auth flows, offer to validate the real sign-up or sign-in flow159after setup is done. If the environment has browser automation tools, you can160use them. If it does not, give the user a short manual validation checklist161instead.162163## Reference Files164165### Provider References166167- `references/convex-auth.md`168- `references/clerk.md`169- `references/workos-authkit.md`170- `references/auth0.md`171172## Checklist173174- [ ] Chosen the correct auth provider before writing setup code175- [ ] Read the relevant provider reference file176- [ ] Asked whether the user wants local-only setup or production-ready setup177- [ ] Used the official provider docs for provider-specific wiring178- [ ] Used the official Convex docs for shared auth behavior and authorization179patterns180- [ ] Only added app-level user storage if the app actually needs it181- [ ] Did not invent a cross-provider `users` table or `storeUser` flow for182Convex Auth183- [ ] Added authentication checks in protected backend functions184- [ ] Added authorization checks where the app actually needs them185- [ ] Clear error messages ("Not authenticated", "Unauthorized")186- [ ] Client auth provider configured for the chosen provider187- [ ] If requested, production auth setup is covered too188