> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ko/llms.txt # 인증0 그만큼`@mastra/auth-auth0`패키지는 Auth0을 사용하여 Mastra에 대한 인증을 제공합니다. Auth0에서 발급한 JWT 토큰을 사용하여 들어오는 요청을 확인하고 다음을 사용하여 Mastra 서버와 통합합니다.`auth`옵션. ## 전제조건 이 예에서는 Auth0 인증을 사용합니다. 다음을 확인하세요. 1. 다음에서 Auth0 계정을 만드세요.[auth0.com](https://auth0.com/) 2. Auth0 대시보드에서 애플리케이션 설정 3. 식별자(대상)를 사용하여 Auth0 대시보드에서 API를 구성합니다. 4. 애플리케이션에 허용된 콜백 URL, 웹 원본 및 로그아웃 URL을 구성합니다. ```env AUTH0_DOMAIN=your-tenant.auth0.com AUTH0_AUDIENCE=your-api-identifier ``` > **노트:** 애플리케이션 > 설정 아래의 Auth0 대시보드에서 도메인을 찾을 수 있습니다. 대상은 Auth0 대시보드 > API에 구성된 API의 식별자입니다. > > 자세한 설정 지침은 사용 중인 플랫폼에 해당하는 [Auth0 빠른 시작](https://auth0.com/docs/quickstarts)을 참조하세요. ## 설치 `MastraAuthAuth0` 클래스를 사용하려면 먼저 `@mastra/auth-auth0` 패키지를 설치해야 합니다. **npm**: ```bash npm install @mastra/auth-auth0@latest ``` **pnpm**: ```bash pnpm add @mastra/auth-auth0@latest ``` **Yarn**: ```bash yarn add @mastra/auth-auth0@latest ``` **Bun**: ```bash bun add @mastra/auth-auth0@latest ``` ## 사용 예 ### 환경 변수를 사용한 기본 사용법 ```typescript import { Mastra } from '@mastra/core' import { MastraAuthAuth0 } from '@mastra/auth-auth0' export const mastra = new Mastra({ server: { auth: new MastraAuthAuth0(), }, }) ``` ### 맞춤 구성 ```typescript import { Mastra } from '@mastra/core' import { MastraAuthAuth0 } from '@mastra/auth-auth0' export const mastra = new Mastra({ server: { auth: new MastraAuthAuth0({ domain: process.env.AUTH0_DOMAIN, audience: process.env.AUTH0_AUDIENCE, }), }, }) ``` ## 구성 ### 사용자 인증 기본적으로 `MastraAuthAuth0`는 지정된 대상에 유효한 Auth0 토큰을 가진 모든 인증 사용자를 허용합니다. 토큰 검증에서는 다음을 확인합니다. 1. 토큰이 Auth0에 의해 올바르게 서명되었습니다. 2. 토큰이 만료되지 않았습니다. 3. 토큰 대상은 구성된 대상과 일치합니다. 4. 토큰 발급자가 Auth0 도메인과 일치합니다. 사용자 인증을 맞춤설정하려면 맞춤 설정을 제공하세요.`authorizeUser` function: ```typescript import { MastraAuthAuth0 } from '@mastra/auth-auth0' const auth0Provider = new MastraAuthAuth0({ authorizeUser: async user => { // Custom authorization logic return user.email?.endsWith('@yourcompany.com') || false }, }) ``` 사용 가능한 모든 구성 옵션은 [MastraAuthAuth0](https://mastra.zisheng.pro/ko/reference/auth/auth0)을 참조하세요. ## 클라이언트 측 설정 Auth0 인증을 사용하는 경우 Auth0 React SDK를 설정하고, 사용자를 인증하고, Mastra 요청에 전달할 액세스 토큰을 검색해야 합니다. ### Auth0 React SDK 설정 먼저 애플리케이션에 Auth0 React SDK를 설치하고 구성합니다. **npm**: ```bash npm install @auth0/auth0-react ``` **pnpm**: ```bash pnpm add @auth0/auth0-react ``` **Yarn**: ```bash yarn add @auth0/auth0-react ``` **Bun**: ```bash bun add @auth0/auth0-react ``` ```typescript import React from 'react' import { Auth0Provider } from '@auth0/auth0-react' const Auth0ProviderWithHistory = ({ children }) => { return ( {children} ) } export default Auth0ProviderWithHistory ``` ### 액세스 토큰 검색 Auth0 React SDK를 사용하여 사용자를 인증하고 액세스 토큰을 검색합니다. ```typescript import { useAuth0 } from '@auth0/auth0-react' export const useAuth0Token = () => { const { getAccessTokenSilently } = useAuth0() const getAccessToken = async () => { const token = await getAccessTokenSilently() return token } return { getAccessToken } } ``` > **노트:** 더 많은 인증 방법과 구성 옵션은 [Auth0 React SDK 문서](https://auth0.com/docs/libraries/auth0-react)를 참조하세요. ## 구성`MastraClient` `auth`가 활성화되면 `MastraClient`로 보내는 모든 요청의 `Authorization` 헤더에 유효한 Auth0 액세스 토큰을 포함해야 합니다. ```typescript import { MastraClient } from '@mastra/client-js' export const createMastraClient = (accessToken: string) => { return new MastraClient({ baseUrl: 'https://', headers: { Authorization: `Bearer ${accessToken}`, }, }) } ``` > **정보:** Authorization 헤더에서 액세스 토큰 앞에 `Bearer`를 붙여야 합니다. 더 많은 구성 옵션은 [Mastra Client SDK](https://mastra.zisheng.pro/ko/docs/server/mastra-client)를 참조하세요. ### 인증된 요청 만들기 Auth0 액세스 토큰으로 `MastraClient`를 구성한 후 인증된 요청을 보낼 수 있습니다. **React**: ```tsx import React, { useState } from 'react' import { useAuth0 } from '@auth0/auth0-react' import { MastraClient } from '@mastra/client-js' export const MastraApiTest = () => { const { getAccessTokenSilently } = useAuth0() const [result, setResult] = useState(null) const callMastraApi = async () => { const token = await getAccessTokenSilently() const mastra = new MastraClient({ baseUrl: 'http://localhost:4111', headers: { Authorization: `Bearer ${token}`, }, }) const weatherAgent = mastra.getAgent('weatherAgent') const response = await weatherAgent.generate("What's the weather like in New York") setResult(response.text) } return (
{result && (
Result:
{result}
)}
) } ``` **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" }' ```