auth/clerk/README.md
A Mastra authentication provider for Clerk, enabling seamless integration of Clerk authentication with Mastra applications.
npm install @mastra/auth-clerk
# or
yarn add @mastra/auth-clerk
# or
pnpm add @mastra/auth-clerk
import { Mastra } from '@mastra/core/mastra';
import { MastraAuthClerk } from '@mastra/auth-clerk';
// Initialize the Clerk auth provider
const clerkAuth = new MastraAuthClerk({
jwksUri: 'your-jwks-uri',
secretKey: 'your-secret-key',
publishableKey: 'your-publishable-key',
});
// Or use environment variables
const clerkAuth = new MastraAuthClerk();
// Enable auth in Mastra
const mastra = new Mastra({
...
server: {
auth: clerkAuth,
},
});
The package can be configured either through constructor options or environment variables:
CLERK_JWKS_URI: The JWKS URI for your Clerk instanceCLERK_SECRET_KEY: Your Clerk secret keyCLERK_PUBLISHABLE_KEY: Your Clerk publishable keyinterface MastraAuthClerkOptions {
jwksUri?: string;
secretKey?: string;
publishableKey?: string;
}
authenticateToken(token: string): Promise<ClerkUser | null>Verifies a JWT token and returns the associated user if valid.
authorizeUser(user: ClerkUser): Promise<boolean>Checks if a user is authorized by verifying their organization membership.