Enforce Authorization in Convex Applications
Verify Convex identity in server functions and test tenant and resource policies across queries, mutations, and actions.
List the exposed queries, mutations and actions that access workspace data. For each, specify caller identity, required membership, permitted roles, object ownership, allowed fields, and expected failure behavior. Start with one simple rule, such as active workspace members may view a record and editors may change its title. Include any action or nested call that can reach the same data through another path. ## Key Points - [Convex auth in functions](https://docs.convex.dev/auth/functions-auth) - [Convex authentication overview](https://docs.convex.dev/auth/overview)
skilldb get auth-patterns-skills/convex-authorizationFull skill: 30 linesEnforce Authorization in Convex Applications
Map public functions to policy
List the exposed queries, mutations and actions that access workspace data. For each, specify caller identity, required membership, permitted roles, object ownership, allowed fields, and expected failure behavior. Start with one simple rule, such as active workspace members may view a record and editors may change its title. Include any action or nested call that can reach the same data through another path.
Use server-side ctx.auth.getUserIdentity() in the documented Convex function context. Decide whether a missing identity is allowed for that operation. If identity is present, use the documented tokenIdentifier or an explicit issuer and subject pair according to the configured authentication setup, then map it to a user row. Avoid treating an editable display name or email as a stable primary key. Validate the function arguments independently, then derive the tenant from membership and the stored target object. A client-supplied workspace ID can identify a lookup but cannot grant access.
Check access where the data changes
For reads, check authorization before returning the row or subscribing the caller to a list. Apply workspace constraints to the database lookup and ensure the result projection omits fields the caller cannot see. For updates, check membership and role close to the write, using the same server-side data that resolves the object. Return a minimal safe failure for unauthorized callers. If the application intentionally hides resource existence, use a consistent outward result for missing and inaccessible records.
When an action calls a separate data function, preserve the authorization boundary in the receiving operation. Review internal function exports and callers so no public path can accidentally invoke privileged behavior with unchecked IDs. Centralize repeated policy logic where useful, while keeping each operation's additional conditions explicit. Log the operation and outcome according to the application's privacy rules without storing token contents.
Verify decisions against fixtures
Create two workspaces with visually similar records, an editor, a read-only member, and a user who belongs to neither. Confirm allowed reads and writes, and that changing a workspace argument cannot cross tenant boundaries. For a denied write, assert the original value still exists and no follow-on work was triggered. Test a deleted membership and any invite or suspended state the application supports. Re-run authorization checks for reactive subscriptions after changing membership so access lifecycle is covered.
Record the function, identity class, workspace and expected policy outcome for each fixture. Keep one integration check that uses the actual configured authentication setup alongside tests with synthetic identities.
References
Install this skill directly: skilldb add auth-patterns-skills
Related Skills
JWT Tokens
JWT creation, validation, refresh token rotation, and secure token storage patterns
Multi Tenancy
Multi-tenant authentication isolation, tenant-scoped tokens, and data boundary enforcement
Oauth2 Flows
OAuth 2.0 authorization code, PKCE, client credentials, and token exchange patterns
Passkeys
Passkeys and WebAuthn implementation for passwordless authentication
Rbac
Role-based access control design, permission hierarchies, and enforcement patterns
Session Management
Server-side session handling, cookie security, session stores, and fixation prevention