Skip to main content

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 example
Direct link to Usage example

src/mastra/index.ts
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 organization
Direct 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.

src/mastra/index.ts
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 ID
Direct 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 parameters
Direct 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.

publishableKey?:

string
= process.env.CLERK_PUBLISHABLE_KEY
Your Clerk publishable key. Can be found in your Clerk Dashboard under API Keys.

secretKey?:

string
= process.env.CLERK_SECRET_KEY
Your Clerk secret key. Used for server-side authentication and token verification.

jwksUri?:

string
= process.env.CLERK_JWKS_URI
The JWKS URI from your Clerk application. Used to verify JWT signatures.

oauthClientId?:

string
= process.env.CLERK_OAUTH_CLIENT_ID
Clerk OAuth application client ID. Required together with oauthClientSecret to enable Studio SSO.

oauthClientSecret?:

string
= process.env.CLERK_OAUTH_CLIENT_SECRET
Clerk OAuth application client secret. Required together with oauthClientId to enable Studio SSO.

redirectUri?:

string
= process.env.CLERK_OAUTH_REDIRECT_URI
Fallback OAuth callback URI when getLoginUrl is called without a redirect URI. The Mastra SSO route supplies its callback URI, which must be registered with Clerk.

scopes?:

string[]
= ['openid', 'profile', 'email']
OAuth scopes requested during SSO login.

session?:

MastraAuthClerkSessionOptions
Encrypted session cookie configuration for Studio SSO.
MastraAuthClerkSessionOptions

cookieName?:

string
Name of the session cookie.

cookieMaxAge?:

number
Session cookie lifetime in seconds.

cookiePassword?:

string
Password for encrypting sessions and signing OAuth state. Must be at least 32 characters when SSO is enabled. If omitted, a generated password is used and sessions do not survive restarts. Use the same password across replicas.

secureCookies?:

boolean
Set the Secure flag on session cookies.

organizationId?:

string
= process.env.CLERK_ORGANIZATION_ID
Restrict login to members of a single Clerk organization, identified by its ID. Users who are not members of this organization are denied access.

organizationSlug?:

string
= process.env.CLERK_ORGANIZATION_SLUG
Restrict login to members of a single Clerk organization, identified by its slug. Users who are not members of this organization are denied access.

name?:

string
Custom name for the auth provider instance.

authorizeUser?:

(user: JwtPayload, request: MastraAuthRequest) => Promise<boolean> | boolean
Custom authorization function to determine if a user should be granted access. For token requests, user holds the verified Clerk JWT claims, identified by sub (JwtPayload is exported from @mastra/auth). When SSO is configured, requests authenticated by the SSO session cookie pass the session user instead, identified by id. Called after token verification. By default, allows all authenticated users, unless organizationId or organizationSlug is set, in which case only members of that organization are allowed.

MastraAuthClerk Class

On this page