> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ko/llms.txt # 사무원 그만큼`@mastra/auth-clerk`패키지는 Clerk를 사용하여 Mastra에 대한 인증을 제공합니다. Clerk의 인증 시스템을 사용하여 들어오는 요청을 확인하고 다음을 사용하여 Mastra 서버와 통합합니다.`auth`옵션. ## 전제조건 이 예제에서는 Clerk 인증을 사용합니다. Clerk 자격 증명을 `.env` 파일에 추가하고 Clerk 프로젝트가 올바르게 구성되어 있는지 확인하세요. ```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 ``` > **노트:** 이 키는 직원 대시보드의 "API 키" 아래에서 찾을 수 있습니다. ## 설치 `MastraAuthClerk` 클래스를 사용하기 전에 `@mastra/auth-clerk` 패키지를 설치해야 합니다. **npm**: ```bash npm install @mastra/auth-clerk@latest ``` **pnpm**: ```bash pnpm add @mastra/auth-clerk@latest ``` **Yarn**: ```bash yarn add @mastra/auth-clerk@latest ``` **Bun**: ```bash bun add @mastra/auth-clerk@latest ``` ## 사용예 ```typescript 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, }), }, }) ``` > **정보:** 기본 `authorizeUser` 메서드는 인증된 모든 사용자를 허용합니다. 사용자 권한 부여를 맞춤 설정하려면 Provider를 생성할 때 사용자 지정 `authorizeUser` 함수를 제공하세요. 사용 가능한 모든 구성 옵션은 [MastraAuthClerk](https://mastra.zisheng.pro/ko/reference/auth/clerk)를 참조하세요. ## 클라이언트 측 설정 Clerk 인증을 사용하는 경우 클라이언트 측 Clerk에서 액세스 토큰을 검색하여 Mastra 요청에 전달해야 합니다. ### 액세스 토큰 검색 Clerk React 후크를 사용하여 사용자를 인증하고 액세스 토큰을 검색합니다. ```typescript import { useAuth } from '@clerk/nextjs' export const useClerkAuth = () => { const { getToken } = useAuth() const getAccessToken = async () => { const token = await getToken() return token } return { getAccessToken } } ``` 자세한 내용은 [Clerk 문서](https://clerk.com/docs)를 참조하세요. ## 구성`MastraClient` `auth`가 활성화되면 `MastraClient`로 보내는 모든 요청의 `Authorization` 헤더에 유효한 Clerk 액세스 토큰을 포함해야 합니다. ```typescript import { MastraClient } from '@mastra/client-js' export const mastraClient = new MastraClient({ baseUrl: 'https://', headers: { Authorization: `Bearer ${accessToken}`, }, }) ``` > **정보:** Authorization 헤더의 액세스 토큰 앞에는 `Bearer`를 붙여야 합니다. 더 많은 구성 옵션은 [Mastra Client SDK](https://mastra.zisheng.pro/ko/docs/server/mastra-client)를 참조하세요. ### 인증된 요청 만들기 Clerk 액세스 토큰으로 `MastraClient`를 구성하면 인증된 요청을 보낼 수 있습니다. **React**: ```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 } ``` **cURL**: ```bash curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \ -H "Content-Type: application/json" \ -H "Authorization: Bearer " \ -d '{ "messages": "Weather in London" }' ```