Auth context
Auth
Thectx.auth object is available in queries, mutations, and actions.
getUserIdentity
Get details about the currently authenticated user.A
UserIdentity object if the user is authenticated, or null if not.- In queries, mutations, and actions: returns
nullif not authenticated - In HTTP actions: throws an error if not authenticated
User identity
UserIdentity
Information about an authenticated user, derived from their JWT.A stable and globally unique identifier for this user. No other user, even from a different identity provider, will have the same identifier.Tip: Use this as your user ID or to look up users in your database.Derived from JWT claims:
sub + issIdentifier for the user from the identity provider. Not necessarily unique across different providers.JWT claim:
subThe hostname of the identity provider that authenticated this user.JWT claim:
issThe user’s email address.JWT claim:
emailWhether the email address has been verified.JWT claim:
email_verifiedThe user’s full name.JWT claim:
nameThe user’s given (first) name.JWT claim:
given_nameThe user’s family (last) name.JWT claim:
family_nameThe user’s nickname.JWT claim:
nicknameThe user’s preferred username.JWT claim:
preferred_usernameURL to the user’s profile page.JWT claim:
profileURL to the user’s profile picture.JWT claim:
pictureThe user’s phone number.JWT claim:
phone_numberWhether the phone number has been verified.JWT claim:
phone_number_verifiedThe user’s gender.JWT claim:
genderThe user’s birthday.JWT claim:
birthdateThe user’s timezone.JWT claim:
zoneinfoThe user’s preferred language.JWT claim:
localeThe user’s address.JWT claim:
addressWhen the user information was last updated.JWT claim:
updated_atCustom claims
Access custom JWT claims by asserting their type:Common patterns
Require authentication
Look up user in database
Create user on first login
Role-based access control
Auth configuration
AuthConfig
Configure authentication providers inconvex/auth.config.ts:
Array of authentication providers that can issue JWTs for your app.
OIDC provider
The domain of the OIDC auth provider.
Tokens must have this application ID in their audiences.
Custom JWT provider
Identifies this as a custom JWT provider.
The issuer of the JWT (e.g.,
https://auth.example.com).URL to fetch the JWKS (e.g.,
https://auth.example.com/.well-known/jwks.json).The algorithm used to sign JWT tokens.
Tokens must have this application ID in their audiences.Warning: Omitting applicationID is often insecure.
Best practices
Always check authentication
For protected operations, always check that
getUserIdentity() returns a non-null value.Use tokenIdentifier as user ID
The
tokenIdentifier is stable and unique across all users and providers. Use it to identify users in your database.Store user data separately
Don’t rely solely on JWT claims. Create a users table and store important user data there.
Validate custom claims
When using custom JWT claims, validate and type-assert them appropriately.