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