MastraAuthClerk class
The MastraAuthClerk class verifies Clerk-issued JWT tokens and supports Studio single sign-on (SSO) through OAuth with encrypted session cookies. Use server.auth for API authentication or studio.auth for separate Studio authentication. See Clerk Studio SSO for setup instructions.
Usage exampleDirect link to Usage example
import { Mastra } from '@mastra/core'
import { MastraAuthClerk } from '@mastra/auth-clerk'
export const mastra = new Mastra({
server: {
auth: new MastraAuthClerk({
jwksUri: process.env.CLERK_JWKS_URI,
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
secretKey: process.env.CLERK_SECRET_KEY,
}),
},
})
Restricting login to a single organizationDirect link to Restricting login to a single organization
Set organizationId or organizationSlug to only allow members of a specific Clerk organization to sign in. Non-members are rejected during authorization. Configure only one of the two. Setting both throws at startup, so login can't be granted to two different organizations.
import { Mastra } from '@mastra/core'
import { MastraAuthClerk } from '@mastra/auth-clerk'
export const mastra = new Mastra({
server: {
auth: new MastraAuthClerk({
jwksUri: process.env.CLERK_JWKS_URI,
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
secretKey: process.env.CLERK_SECRET_KEY,
organizationId: process.env.CLERK_ORGANIZATION_ID,
}),
},
})
User IDDirect link to User ID
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.
Constructor parametersDirect link to Constructor parameters
publishableKey, secretKey, and jwksUri must each be supplied either as a constructor option or through the corresponding environment variable. They're optional as arguments, not as credentials. Construction throws if any of the three is missing. Studio SSO additionally requires both oauthClientId and oauthClientSecret.