8.2 KiB
Convex Auth
Official docs: https://docs.convex.dev/auth/convex-auth Setup guide: https://labs.convex.dev/auth/setup
Use this when the user wants auth handled directly in Convex rather than through a third-party provider.
Workflow
- Confirm the user wants Convex Auth specifically
- Determine which sign-in methods the app needs:
- magic links or OTPs
- OAuth providers
- passwords and password reset
- Ask whether the user wants local-only setup or production-ready setup now
- Read the Convex Auth setup guide before writing code
- Make sure the project has a configured Convex deployment:
- run
npx convex devfirst ifCONVEX_DEPLOYMENTis not set - if CLI configuration requires interactive human input, stop and ask the user to complete that step before continuing
- run
- Install the auth packages:
npm install @convex-dev/auth @auth/core@0.37.0
- Run the initialization command:
npx @convex-dev/auth
- Confirm the initializer created:
convex/auth.config.tsconvex/auth.tsconvex/http.ts
- Add the required
authTablestoconvex/schema.ts - Replace plain
ConvexProviderwiring withConvexAuthProvider - Configure at least one auth method in
convex/auth.ts - Run
npx convex dev --onceor the normal dev flow to push the updated schema and generated code - Verify the client can sign in successfully
- Verify Convex receives authenticated identity in backend functions
- If the user wants production-ready setup, make sure the same auth setup is configured for the production deployment as well
- Only add a
userstable andstoreUserflow if the app needs app-level user records inside Convex
What This Reference Is For
- choosing Convex Auth as the default provider for a new Convex app
- understanding whether the app wants magic links, OTPs, OAuth, or passwords
- keeping the setup provider-specific while using the official Convex Auth docs for identity and authorization behavior
What To Do
- Read the Convex Auth setup guide before writing setup code
- Follow the setup flow from the docs rather than recreating it from memory
- If the app is new, consider starting from the official starter flow instead of hand-wiring everything
- Treat
npx @convex-dev/authas a required initialization step for existing apps, not an optional extra
Concrete Steps
- Install
@convex-dev/authand@auth/core@0.37.0 - Run
npx convex devif the project does not already have a configured deployment - If
npx convex devblocks on interactive setup, ask the user explicitly to finish configuring the Convex deployment - Run
npx @convex-dev/auth - Confirm the generated auth setup is present before continuing:
convex/auth.config.tsconvex/auth.tsconvex/http.ts
- Add
authTablestoconvex/schema.ts - Replace
ConvexProviderwithConvexAuthProviderin the app entry - Configure the selected auth methods in
convex/auth.ts - Run
npx convex dev --onceor the normal dev flow so the updated schema and auth files are pushed - Verify login locally
- If the user wants production-ready setup, repeat the required auth configuration against the production deployment
Expected Files and Decisions
-
convex/schema.ts -
frontend app entry such as
src/main.tsxor the framework-equivalent provider file -
generated Convex Auth setup produced by
npx @convex-dev/auth -
an existing configured Convex deployment, or the ability to create one with
npx convex dev -
convex/auth.tsstarts withproviders: []until the app configures actual sign-in methods -
Decide whether the user is creating a new app or adding auth to an existing app
-
For a new app, prefer the official starter flow instead of rebuilding setup by hand
-
Decide which auth methods the app needs:
- magic links or OTPs
- OAuth providers
- passwords
-
Decide whether the user wants local-only setup or production-ready setup now
-
Decide whether the app actually needs a
userstable inside Convex, or whether provider identity alone is enough
Gotchas
- Do not assume a specific sign-in method. Ask which methods the app needs before wiring UI and backend behavior.
npx @convex-dev/authis important because it initializes the auth setup, including the key material. Do not skip it when adding Convex Auth to an existing project.npx @convex-dev/authwill fail if the project does not already have a configuredCONVEX_DEPLOYMENT.npx convex devmay require interactive setup for deployment creation or project selection. If that happens, ask the user explicitly for that human step instead of guessing.npx @convex-dev/authdoes not finish the whole integration by itself. You still need to addauthTables, swap inConvexAuthProvider, and configure at least one auth method.- A project can still build even if
convex/auth.tsstill hasproviders: [], so do not treat a successful build as proof that sign-in is fully configured. - Convex Auth does not mean every app needs a
userstable. If the app only needs authentication gates,ctx.auth.getUserIdentity()may be enough. - If the app is greenfield, starting from the official starter flow is usually better than partially recreating it by hand.
- Do not stop at local dev setup if the user expects production-ready auth. The production deployment needs the auth setup too.
- Keep provider-specific setup and Convex Auth authorization behavior in the official docs instead of inventing shared patterns from memory.
Production
- Ask whether the user wants dev-only setup or production-ready setup
- If the answer is production-ready, make sure the auth configuration is applied to the production deployment, not just the dev deployment
- Verify production-specific redirect URLs, auth method configuration, and deployment settings before calling the task complete
- Do not silently write a notes file into the repo by default. If the user wants rollout or handoff docs, create one explicitly.
Human Handoff
If npx convex dev or deployment setup requires human input:
- stop and explain exactly what the user needs to do
- say why that step is required
- resume the auth setup immediately after the user confirms it is done
Validation
- Verify the user can complete a sign-in flow
- Offer to validate sign up, sign out, and sign back in with the configured auth method
- If browser automation is available in the environment, you can do this directly
- If browser automation is not available, give the user a short manual validation checklist instead
- Verify
ctx.auth.getUserIdentity()returns an identity in protected backend functions - Verify protected UI only renders after Convex-authenticated state is ready
- Verify environment variables and redirect settings match the current app environment
- Verify
convex/auth.tsno longer has an emptyproviders: []configuration once the app is meant to support real sign-in - Run
npx convex dev --onceor the normal dev flow after setup changes and confirm Convex codegen and push succeed - If production-ready setup was requested, verify the production deployment is also configured correctly
Checklist
- Confirm the user wants Convex Auth specifically
- Ask whether the user wants local-only setup or production-ready setup
- Ensure a Convex deployment is configured before running auth initialization
- Install
@convex-dev/authand@auth/core@0.37.0 - Run
npx convex devfirst if needed - Run
npx @convex-dev/auth - Confirm
convex/auth.config.ts,convex/auth.ts, andconvex/http.tswere created - Follow the setup guide for package install and wiring
- Add
authTablestoconvex/schema.ts - Replace
ConvexProviderwithConvexAuthProvider - Configure at least one auth method in
convex/auth.ts - Run
npx convex dev --onceor the normal dev flow after setup changes - Confirm which sign-in methods the app needs
- Verify the client can sign in and the backend receives authenticated identity
- Offer end-to-end validation of sign up, sign out, and sign back in
- If requested, configure the production deployment too
- Only add extra
userstable sync if the app needs app-level user records