Convex Authentication Setup
Implement secure authentication in Convex with user management and access control.
When to Use
- Setting up authentication for the first time
- Implementing user management (users table, identity mapping)
- Creating authentication helper functions
- Setting up auth providers (Convex Auth, Clerk, WorkOS AuthKit, Auth0, custom JWT)
When Not to Use
- Auth for a non-Convex backend
- Pure OAuth/OIDC documentation without a Convex implementation
- Debugging unrelated bugs that happen to surface near auth code
- The auth provider is already fully configured and the user only needs a one-line fix
First Step: Choose the Auth Provider
Convex supports multiple authentication approaches. Do not assume a provider.
Before writing setup code:
- Ask the user which auth solution they want, unless the repository already makes it obvious
- If the repo already uses a provider, continue with that provider unless the user wants to switch
- If the user has not chosen a provider and the repo does not make it obvious, ask before proceeding
Common options:
- Convex Auth - good default when the user wants auth handled directly in Convex
- Clerk - use when the app already uses Clerk or the user wants Clerk's hosted auth features
- WorkOS AuthKit - use when the app already uses WorkOS or the user wants AuthKit specifically
- Auth0 - use when the app already uses Auth0
- Custom JWT provider - use when integrating an existing auth system not covered above
Look for signals in the repo before asking:
- Dependencies such as
@clerk/*, @workos-inc/*, @auth0/*, or Convex Auth packages
- Existing files such as
convex/auth.config.ts, auth middleware, provider wrappers, or login components
- Environment variables that clearly point at a provider
After Choosing a Provider
Read the provider's official guide and the matching local reference file:
The local reference files contain the concrete workflow, expected files and env vars, gotchas, and validation checks.
Use those sources for:
- package installation
- client provider wiring
- environment variables
convex/auth.config.ts setup
- login and logout UI patterns
- framework-specific setup for React, Vite, or Next.js
For shared auth behavior, use the official Convex docs as the source of truth:
Prefer official docs over recalled steps, because provider CLIs and Convex Auth internals change between versions. Inventing setup from memory risks outdated patterns.
For third-party providers, only add app-level user storage if the app actually needs user documents in Convex. Not every app needs a users table.
For Convex Auth, follow the Convex Auth docs and built-in auth tables rather than adding a parallel users table plus storeUser flow, because Convex Auth already manages user records internally.
After running provider initialization commands, verify generated files and complete the post-init wiring steps the provider reference calls out. Initialization commands rarely finish the entire integration.
Core Pattern: Protecting Backend Functions
The most common auth task is checking identity in Convex functions.
// Bad: trusting a client-provided userId
export const getMyProfile = query({
args: { userId: v.id("users") },
handler: async (ctx, args) => {
return await ctx.db.get(args.userId);
},
});
// Good: verifying identity server-side
export const getMyProfile = query({
args: {},
handler: async (ctx) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) throw new Error("Not authenticated");
return await ctx.db
.query("users")
.withIndex("by_tokenIdentifier", (q) =>
q.eq("tokenIdentifier", identity.tokenIdentifier)
)
.unique();
},
});
Workflow
- Determine the provider, either by asking the user or inferring from the repo
- Ask whether the user wants local-only setup or production-ready setup now
- Read the matching provider reference file
- Follow the official provider docs for current setup details
- Follow the official Convex docs for shared backend auth behavior, user storage, and authorization patterns
- Only add app-level user storage if the docs and app requirements call for it
- Add authorization checks for ownership, roles, or team access only where the app needs them
- Verify login state, protected queries, environment variables, and production configuration if requested
If the flow blocks on interactive provider or deployment setup, ask the user explicitly for the exact human step needed, then continue after they complete it.
For UI-facing auth flows, offer to validate the real sign-up or sign-in flow after setup is done.
If the environment has browser automation tools, you can use them.
If it does not, give the user a short manual validation checklist instead.
Reference Files
Provider References
references/convex-auth.md
references/clerk.md
references/workos-authkit.md
references/auth0.md
Checklist
1---
2name: convex-setup-auth
3description: Sets up Convex authentication with user management, identity mapping, and access control. Use this skill when adding login or signup to a Convex app, configuring Convex Auth, Clerk, WorkOS AuthKit, Auth0, or custom JWT providers, wiring auth.config.ts, protecting queries and mutations with ctx.auth.getUserIdentity(), creating a users table with identity mapping, or setting up role-based access control, even if the user just says "add auth" or "make it require login."
4metadata:
5 source: https://skillmd.com/skills/aibot88/convex-setup-auth
6---
7
8# Convex Authentication Setup
9
10Implement secure authentication in Convex with user management and access control.
11
12## When to Use
13
14- Setting up authentication for the first time
15- Implementing user management (users table, identity mapping)
16- Creating authentication helper functions
17- Setting up auth providers (Convex Auth, Clerk, WorkOS AuthKit, Auth0, custom JWT)
18
19## When Not to Use
20
21- Auth for a non-Convex backend
22- Pure OAuth/OIDC documentation without a Convex implementation
23- Debugging unrelated bugs that happen to surface near auth code
24- The auth provider is already fully configured and the user only needs a one-line fix
25
26## First Step: Choose the Auth Provider
27
28Convex supports multiple authentication approaches. Do not assume a provider.
29
30Before writing setup code:
31
321. Ask the user which auth solution they want, unless the repository already makes it obvious
332. If the repo already uses a provider, continue with that provider unless the user wants to switch
343. If the user has not chosen a provider and the repo does not make it obvious, ask before proceeding
35
36Common options:
37
38- [Convex Auth](https://docs.convex.dev/auth/convex-auth) - good default when the user wants auth handled directly in Convex
39- [Clerk](https://docs.convex.dev/auth/clerk) - use when the app already uses Clerk or the user wants Clerk's hosted auth features
40- [WorkOS AuthKit](https://docs.convex.dev/auth/authkit/) - use when the app already uses WorkOS or the user wants AuthKit specifically
41- [Auth0](https://docs.convex.dev/auth/auth0) - use when the app already uses Auth0
42- Custom JWT provider - use when integrating an existing auth system not covered above
43
44Look for signals in the repo before asking:
45
46- Dependencies such as `@clerk/*`, `@workos-inc/*`, `@auth0/*`, or Convex Auth packages
47- Existing files such as `convex/auth.config.ts`, auth middleware, provider wrappers, or login components
48- Environment variables that clearly point at a provider
49
50## After Choosing a Provider
51
52Read the provider's official guide and the matching local reference file:
53
54- Convex Auth: [official docs](https://docs.convex.dev/auth/convex-auth), then `references/convex-auth.md`
55- Clerk: [official docs](https://docs.convex.dev/auth/clerk), then `references/clerk.md`
56- WorkOS AuthKit: [official docs](https://docs.convex.dev/auth/authkit/), then `references/workos-authkit.md`
57- Auth0: [official docs](https://docs.convex.dev/auth/auth0), then `references/auth0.md`
58
59The local reference files contain the concrete workflow, expected files and env vars, gotchas, and validation checks.
60
61Use those sources for:
62
63- package installation
64- client provider wiring
65- environment variables
66- `convex/auth.config.ts` setup
67- login and logout UI patterns
68- framework-specific setup for React, Vite, or Next.js
69
70For shared auth behavior, use the official Convex docs as the source of truth:
71
72- [Auth in Functions](https://docs.convex.dev/auth/functions-auth) for `ctx.auth.getUserIdentity()`
73- [Storing Users in the Convex Database](https://docs.convex.dev/auth/database-auth) for optional app-level user storage
74- [Authentication](https://docs.convex.dev/auth) for general auth and authorization guidance
75- [Convex Auth Authorization](https://labs.convex.dev/auth/authz) when the provider is Convex Auth
76
77Prefer official docs over recalled steps, because provider CLIs and Convex Auth internals change between versions. Inventing setup from memory risks outdated patterns.
78For third-party providers, only add app-level user storage if the app actually needs user documents in Convex. Not every app needs a `users` table.
79For Convex Auth, follow the Convex Auth docs and built-in auth tables rather than adding a parallel `users` table plus `storeUser` flow, because Convex Auth already manages user records internally.
80After running provider initialization commands, verify generated files and complete the post-init wiring steps the provider reference calls out. Initialization commands rarely finish the entire integration.
81
82## Core Pattern: Protecting Backend Functions
83
84The most common auth task is checking identity in Convex functions.
85
86```ts
87// Bad: trusting a client-provided userId
88export const getMyProfile = query({
89 args: { userId: v.id("users") },
90 handler: async (ctx, args) => {
91 return await ctx.db.get(args.userId);
92 },
93});
94```
95
96```ts
97// Good: verifying identity server-side
98export const getMyProfile = query({
99 args: {},
100 handler: async (ctx) => {
101 const identity = await ctx.auth.getUserIdentity();
102 if (!identity) throw new Error("Not authenticated");
103
104 return await ctx.db
105 .query("users")
106 .withIndex("by_tokenIdentifier", (q) =>
107 q.eq("tokenIdentifier", identity.tokenIdentifier)
108 )
109 .unique();
110 },
111});
112```
113
114## Workflow
115
1161. Determine the provider, either by asking the user or inferring from the repo
1172. Ask whether the user wants local-only setup or production-ready setup now
1183. Read the matching provider reference file
1194. Follow the official provider docs for current setup details
1205. Follow the official Convex docs for shared backend auth behavior, user storage, and authorization patterns
1216. Only add app-level user storage if the docs and app requirements call for it
1227. Add authorization checks for ownership, roles, or team access only where the app needs them
1238. Verify login state, protected queries, environment variables, and production configuration if requested
124
125If the flow blocks on interactive provider or deployment setup, ask the user explicitly for the exact human step needed, then continue after they complete it.
126For UI-facing auth flows, offer to validate the real sign-up or sign-in flow after setup is done.
127If the environment has browser automation tools, you can use them.
128If it does not, give the user a short manual validation checklist instead.
129
130## Reference Files
131
132### Provider References
133
134- `references/convex-auth.md`
135- `references/clerk.md`
136- `references/workos-authkit.md`
137- `references/auth0.md`
138
139## Checklist
140
141- [ ] Chosen the correct auth provider before writing setup code
142- [ ] Read the relevant provider reference file
143- [ ] Asked whether the user wants local-only setup or production-ready setup
144- [ ] Used the official provider docs for provider-specific wiring
145- [ ] Used the official Convex docs for shared auth behavior and authorization patterns
146- [ ] Only added app-level user storage if the app actually needs it
147- [ ] Did not invent a cross-provider `users` table or `storeUser` flow for Convex Auth
148- [ ] Added authentication checks in protected backend functions
149- [ ] Added authorization checks where the app actually needs them
150- [ ] Clear error messages ("Not authenticated", "Unauthorized")
151- [ ] Client auth provider configured for the chosen provider
152- [ ] If requested, production auth setup is covered too