跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Clerk

The @mastra/auth-clerk package provides authentication for Mastra using Clerk. It verifies incoming requests using Clerk's authentication system and integrates with the Mastra server using the auth option.

Prerequisites
Prerequisites的直接链接

This example uses Clerk authentication. Make sure to add your Clerk credentials to your .env file and ensure your Clerk project is properly configured.

.env
CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...
CLERK_JWKS_URI=https://your-clerk-domain.clerk.accounts.dev/.well-known/jwks.json
备注

You can find these keys in your Clerk Dashboard under "API Keys".

Installation
Installation的直接链接

Before you can use the MastraAuthClerk class you have to install the @mastra/auth-clerk package.

npm install @mastra/auth-clerk@latest

Usage example
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({
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
secretKey: process.env.CLERK_SECRET_KEY,
jwksUri: process.env.CLERK_JWKS_URI,
}),
},
})
备注

The default authorizeUser method allows all authenticated users. To customize user authorization, provide a custom authorizeUser function when constructing the provider.

Visit MastraAuthClerk for all available configuration options.

Client-side setup
Client-side setup的直接链接

When using Clerk auth, you'll need to retrieve the access token from Clerk on the client side and pass it to your Mastra requests.

Retrieving the access token
Retrieving the access token的直接链接

Use the Clerk React hooks to authenticate users and retrieve their access token:

lib/auth.ts
import { useAuth } from '@clerk/nextjs'

export const useClerkAuth = () => {
const { getToken } = useAuth()

const getAccessToken = async () => {
const token = await getToken()
return token
}

return { getAccessToken }
}

Refer to the Clerk documentation for more information.

Configuring MastraClient
configuring-mastraclient的直接链接

When auth is enabled, all requests made with MastraClient must include a valid Clerk access token in the Authorization header:

lib/mastra/mastra-client.ts
import { MastraClient } from '@mastra/client-js'

export const mastraClient = new MastraClient({
baseUrl: 'https://<mastra-api-url>',
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
备注

The access token must be prefixed with Bearer in the Authorization header.

Visit Mastra Client SDK for more configuration options.

Making authenticated requests
Making authenticated requests的直接链接

Once MastraClient is configured with the Clerk access token, you can send authenticated requests:

src/components/test-agent.tsx
'use client'

import { useAuth } from '@clerk/nextjs'
import { MastraClient } from '@mastra/client-js'

export const TestAgent = () => {
const { getToken } = useAuth()

async function handleClick() {
const token = await getToken()

const client = new MastraClient({
baseUrl: 'http://localhost:4111',
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
})

const weatherAgent = client.getAgent('weatherAgent')
const response = await weatherAgent.generate("What's the weather like in New York")

console.log({ response })
}

return <button onClick={handleClick}>Test Agent</button>
}