MastraAuthAuth0 class
The MastraAuthAuth0 class provides authentication for Mastra using Auth0. It verifies incoming requests using Auth0-issued JWT tokens and integrates with the Mastra server using the auth option.
Usage exampleDirect link to Usage example
import { Mastra } from '@mastra/core'
import { MastraAuthAuth0 } from '@mastra/auth-auth0'
export const mastra = new Mastra({
server: {
auth: new MastraAuthAuth0({
domain: process.env.AUTH0_DOMAIN,
audience: process.env.AUTH0_AUDIENCE,
}),
},
})
You can omit the constructor parameters if the required environment variables (AUTH0_DOMAIN and AUTH0_AUDIENCE) are set. In that case, use new MastraAuthAuth0() without any arguments.
Constructor parametersDirect link to Constructor parameters
domain?:
audience?:
name?:
Environment variablesDirect link to Environment variables
The following environment variables are automatically used when constructor options aren't provided:
AUTH0_DOMAIN?:
AUTH0_AUDIENCE?:
Default authorization behaviorDirect link to Default authorization behavior
By default, MastraAuthAuth0 validates Auth0 JWT tokens and allows access to all authenticated users:
- Token Verification: The JWT token is verified using Auth0's public keys (JWKS)
- Signature Validation: Ensures the token was signed by your Auth0 tenant
- Expiration Check: Verifies the token hasn't expired
- Audience Validation: Confirms the token was issued for your specific API (audience)
- Issuer Validation: Ensures the token was issued by your Auth0 domain
If all validations pass, the user is considered authorized. To implement custom authorization logic (e.g., role-based access control), provide a custom authorizeUser function.
Auth0 user typeDirect link to Auth0 user type
The Auth0User type used in the authorizeUser function corresponds to the decoded JWT token payload, which typically includes:
sub: The user's unique identifier (subject)email: The user's email address (if included in token)email_verified: Whether the email is verifiedname: The user's display name (if available)picture: URL to the user's profile picture (if available)iss: Token issuer (your Auth0 domain)aud: Token audience (your API identifier)iat: Token issued at timestampexp: Token expiration timestampscope: Granted scopes for the token- Custom claims and app metadata configured in your Auth0 tenant
The exact properties available depend on your Auth0 configuration, scopes requested, and any custom claims you've configured.
The authenticated user's id is the token's id claim, or its sub claim when the token has no id. RBAC looks up roles by this id, and it owns the records the user creates unless mapUserToResourceId is set. See user IDs from JWT providers.