auth.config.ts and access user identity in your functions.
Configuration
Define your authentication providers inauth.config.ts:
Auth config
TheAuthConfig type defines authentication configuration:
An array of authentication providers allowed to issue JWTs for your app.
Auth providers
Convex supports two types of authentication providers:OIDC provider
The domain of the OIDC auth provider.
Tokens issued by the auth provider must have this application ID in their audiences.
Custom JWT provider
Indicates this is a custom JWT provider.
The issuer of the JWT auth provider (e.g.,
https://auth.example.com).The URL to fetch the JWKS (JSON Web Key Set) for token verification.
The algorithm used to sign JWT tokens. Convex currently supports RS256 and ES256.
Tokens must have this application ID in their audiences. Warning: Omitting applicationID is often insecure.
User identity
Access authenticated user information viactx.auth in queries, mutations, and actions.
Get user identity
UserIdentity interface
TheUserIdentity object contains information derived from the JWT token. Only tokenIdentifier and issuer are guaranteed to be present - all other fields depend on what the identity provider includes.
Standard fields
These fields are derived from OpenID Connect (OIDC) standard claims:A stable and globally unique string for this identity. No other user, even from a different provider, will have the same string. Derived from JWT claims
sub + iss.Identifier for the end-user from the identity provider, not necessarily unique across different providers. JWT claim:
sub.The hostname of the identity provider used to authenticate this user. JWT claim:
iss.The user’s full name. JWT claim:
name.The user’s given (first) name. JWT claim:
given_name.The user’s family (last) name. JWT claim:
family_name.The user’s nickname or username. JWT claim:
nickname.The user’s preferred username. JWT claim:
preferred_username.URL of the user’s profile page. JWT claim:
profile.URL of the user’s profile picture. JWT claim:
picture.The user’s email address. JWT claim:
email.Whether the email address has been verified. JWT claim:
email_verified.The user’s gender. JWT claim:
gender.The user’s birthday. JWT claim:
birthdate.The user’s timezone. JWT claim:
zoneinfo.The user’s preferred language. JWT claim:
locale.The user’s phone number. JWT claim:
phone_number.Whether the phone number has been verified. JWT claim:
phone_number_verified.The user’s address. JWT claim:
address.When the user’s information was last updated. JWT claim:
updated_at.Custom claims
Any additional custom claims from your JWT are also available. Type assert them if you know their type:Auth interface
TheAuth interface is available as ctx.auth in all Convex functions:
getUserIdentity
Get details about the currently authenticated user:UserIdentity object if the Convex client was configured with a valid ID token, otherwise:
- Returns
nullin queries, mutations, and actions - Throws in HTTP actions
Common patterns
Require authentication
Throw an error if the user is not authenticated:Link users to documents
Store thetokenIdentifier to link documents to users:
Role-based access control
Implement custom roles using custom JWT claims:Best practices
- Always check authentication when required -
getUserIdentity()can returnnull. - Use
tokenIdentifierfor user linking - It’s stable and globally unique. - Configure
applicationID- Omitting it in custom JWT providers is often insecure. - Store minimal user data - Only store what you need from the identity in your database.
- Use internal mutations for admin operations - Don’t expose sensitive operations as public functions.