@mastra/client-js 1.0.0-beta.9 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1652 -0
- package/README.md +1 -3
- package/dist/_types/@ai-sdk_ui-utils/dist/index.d.ts +820 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +8511 -0
- package/dist/client.d.ts +56 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/docs/README.md +33 -0
- package/dist/docs/SKILL.md +34 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/ai-sdk/01-reference.md +358 -0
- package/dist/docs/client-js/01-reference.md +1180 -0
- package/dist/docs/server/01-mastra-client.md +256 -0
- package/dist/docs/server/02-jwt.md +99 -0
- package/dist/docs/server/03-clerk.md +143 -0
- package/dist/docs/server/04-supabase.md +128 -0
- package/dist/docs/server/05-firebase.md +286 -0
- package/dist/docs/server/06-workos.md +201 -0
- package/dist/docs/server/07-auth0.md +233 -0
- package/dist/index.cjs +1690 -596
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1688 -594
- package/dist/index.js.map +1 -1
- package/dist/resources/agent-builder.d.ts +10 -26
- package/dist/resources/agent-builder.d.ts.map +1 -1
- package/dist/resources/agent.d.ts +43 -8
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/index.d.ts +1 -0
- package/dist/resources/index.d.ts.map +1 -1
- package/dist/resources/memory-thread.d.ts +18 -3
- package/dist/resources/memory-thread.d.ts.map +1 -1
- package/dist/resources/observability.d.ts +58 -15
- package/dist/resources/observability.d.ts.map +1 -1
- package/dist/resources/processor.d.ts +20 -0
- package/dist/resources/processor.d.ts.map +1 -0
- package/dist/resources/run.d.ts +210 -0
- package/dist/resources/run.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +19 -224
- package/dist/resources/workflow.d.ts.map +1 -1
- package/dist/tools.d.ts +2 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/types.d.ts +141 -36
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +26 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +13 -12
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
> Documentation for the MastraAuthAuth0 class, which authenticates Mastra applications using Auth0 authentication.
|
|
2
|
+
|
|
3
|
+
# MastraAuthAuth0 Class
|
|
4
|
+
|
|
5
|
+
The `MastraAuthAuth0` class provides authentication for Mastra using Auth0. It verifies incoming requests using Auth0-issued JWT tokens and integrates with the Mastra server using the `auth` option.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
This example uses Auth0 authentication. Make sure to:
|
|
10
|
+
|
|
11
|
+
1. Create an Auth0 account at [auth0.com](https://auth0.com/)
|
|
12
|
+
2. Set up an Application in your Auth0 Dashboard
|
|
13
|
+
3. Configure an API in your Auth0 Dashboard with an identifier (audience)
|
|
14
|
+
4. Configure your application's allowed callback URLs, web origins, and logout URLs
|
|
15
|
+
|
|
16
|
+
```env title=".env"
|
|
17
|
+
AUTH0_DOMAIN=your-tenant.auth0.com
|
|
18
|
+
AUTH0_AUDIENCE=your-api-identifier
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
> **Note:**
|
|
22
|
+
|
|
23
|
+
You can find your domain in the Auth0 Dashboard under Applications > Settings. The audience is the identifier of your API configured in Auth0 Dashboard > APIs.
|
|
24
|
+
|
|
25
|
+
For detailed setup instructions, refer to the [Auth0 quickstarts](https://auth0.com/docs/quickstarts) for your specific platform.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
Before you can use the `MastraAuthAuth0` class you have to install the `@mastra/auth-auth0` package.
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @mastra/auth-auth0@beta
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage examples
|
|
36
|
+
|
|
37
|
+
### Basic usage with environment variables
|
|
38
|
+
|
|
39
|
+
```typescript {2,6} title="src/mastra/index.ts"
|
|
40
|
+
import { Mastra } from "@mastra/core";
|
|
41
|
+
import { MastraAuthAuth0 } from "@mastra/auth-auth0";
|
|
42
|
+
|
|
43
|
+
export const mastra = new Mastra({
|
|
44
|
+
server: {
|
|
45
|
+
auth: new MastraAuthAuth0(),
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Custom configuration
|
|
51
|
+
|
|
52
|
+
```typescript {2,6-9} title="src/mastra/index.ts"
|
|
53
|
+
import { Mastra } from "@mastra/core";
|
|
54
|
+
import { MastraAuthAuth0 } from "@mastra/auth-auth0";
|
|
55
|
+
|
|
56
|
+
export const mastra = new Mastra({
|
|
57
|
+
server: {
|
|
58
|
+
auth: new MastraAuthAuth0({
|
|
59
|
+
domain: process.env.AUTH0_DOMAIN,
|
|
60
|
+
audience: process.env.AUTH0_AUDIENCE,
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Configuration
|
|
67
|
+
|
|
68
|
+
### User Authorization
|
|
69
|
+
|
|
70
|
+
By default, `MastraAuthAuth0` allows all authenticated users who have valid Auth0 tokens for the specified audience. The token verification ensures that:
|
|
71
|
+
|
|
72
|
+
1. The token is properly signed by Auth0
|
|
73
|
+
2. The token is not expired
|
|
74
|
+
3. The token audience matches your configured audience
|
|
75
|
+
4. The token issuer matches your Auth0 domain
|
|
76
|
+
|
|
77
|
+
To customize user authorization, provide a custom `authorizeUser` function:
|
|
78
|
+
|
|
79
|
+
```typescript title="src/mastra/auth.ts"
|
|
80
|
+
import { MastraAuthAuth0 } from "@mastra/auth-auth0";
|
|
81
|
+
|
|
82
|
+
const auth0Provider = new MastraAuthAuth0({
|
|
83
|
+
authorizeUser: async (user) => {
|
|
84
|
+
// Custom authorization logic
|
|
85
|
+
return user.email?.endsWith("@yourcompany.com") || false;
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
> **Note:**
|
|
91
|
+
|
|
92
|
+
Visit [MastraAuthAuth0](https://mastra.ai/reference/v1/auth/auth0) for all available configuration options.
|
|
93
|
+
|
|
94
|
+
## Client-side setup
|
|
95
|
+
|
|
96
|
+
When using Auth0 auth, you'll need to set up the Auth0 React SDK, authenticate users, and retrieve their access tokens to pass to your Mastra requests.
|
|
97
|
+
|
|
98
|
+
### Setting up Auth0 React SDK
|
|
99
|
+
|
|
100
|
+
First, install and configure the Auth0 React SDK in your application:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm install @auth0/auth0-react
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```typescript title="src/auth0-provider.tsx"
|
|
107
|
+
import React from 'react';
|
|
108
|
+
import { Auth0Provider } from '@auth0/auth0-react';
|
|
109
|
+
|
|
110
|
+
const Auth0ProviderWithHistory = ({ children }) => {
|
|
111
|
+
return (
|
|
112
|
+
<Auth0Provider
|
|
113
|
+
domain={process.env.REACT_APP_AUTH0_DOMAIN}
|
|
114
|
+
clientId={process.env.REACT_APP_AUTH0_CLIENT_ID}
|
|
115
|
+
authorizationParams={{
|
|
116
|
+
redirect_uri: window.location.origin,
|
|
117
|
+
audience: process.env.REACT_APP_AUTH0_AUDIENCE,
|
|
118
|
+
scope: "read:current_user update:current_user_metadata"
|
|
119
|
+
}}
|
|
120
|
+
>
|
|
121
|
+
{children}
|
|
122
|
+
</Auth0Provider>
|
|
123
|
+
);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export default Auth0ProviderWithHistory;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Retrieving access tokens
|
|
130
|
+
|
|
131
|
+
Use the Auth0 React SDK to authenticate users and retrieve their access tokens:
|
|
132
|
+
|
|
133
|
+
```typescript title="lib/auth.ts"
|
|
134
|
+
import { useAuth0 } from "@auth0/auth0-react";
|
|
135
|
+
|
|
136
|
+
export const useAuth0Token = () => {
|
|
137
|
+
const { getAccessTokenSilently } = useAuth0();
|
|
138
|
+
|
|
139
|
+
const getAccessToken = async () => {
|
|
140
|
+
const token = await getAccessTokenSilently();
|
|
141
|
+
return token;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return { getAccessToken };
|
|
145
|
+
};
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
> **Note:**
|
|
149
|
+
|
|
150
|
+
Refer to the [Auth0 React SDK documentation](https://auth0.com/docs/libraries/auth0-react) for more authentication methods and configuration options.
|
|
151
|
+
|
|
152
|
+
## Configuring `MastraClient`
|
|
153
|
+
|
|
154
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid Auth0 access token in the `Authorization` header:
|
|
155
|
+
|
|
156
|
+
```typescript title="lib/mastra/mastra-client.ts"
|
|
157
|
+
import { MastraClient } from "@mastra/client-js";
|
|
158
|
+
|
|
159
|
+
export const createMastraClient = (accessToken: string) => {
|
|
160
|
+
return new MastraClient({
|
|
161
|
+
baseUrl: "https://<mastra-api-url>",
|
|
162
|
+
headers: {
|
|
163
|
+
Authorization: `Bearer ${accessToken}`,
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
> **Note:**
|
|
170
|
+
|
|
171
|
+
The access token must be prefixed with `Bearer` in the Authorization header.
|
|
172
|
+
|
|
173
|
+
Visit [Mastra Client SDK](https://mastra.ai/docs/v1/server/mastra-client) for more configuration options.
|
|
174
|
+
|
|
175
|
+
### Making authenticated requests
|
|
176
|
+
|
|
177
|
+
Once `MastraClient` is configured with the Auth0 access token, you can send authenticated requests:
|
|
178
|
+
|
|
179
|
+
**react:**
|
|
180
|
+
|
|
181
|
+
```tsx title="src/components/mastra-api-test.tsx" copy
|
|
182
|
+
import React, { useState } from 'react';
|
|
183
|
+
import { useAuth0 } from '@auth0/auth0-react';
|
|
184
|
+
import { MastraClient } from '@mastra/client-js';
|
|
185
|
+
|
|
186
|
+
export const MastraApiTest = () => {
|
|
187
|
+
const { getAccessTokenSilently } = useAuth0();
|
|
188
|
+
const [result, setResult] = useState(null);
|
|
189
|
+
|
|
190
|
+
const callMastraApi = async () => {
|
|
191
|
+
const token = await getAccessTokenSilently();
|
|
192
|
+
|
|
193
|
+
const mastra = new MastraClient({
|
|
194
|
+
baseUrl: "http://localhost:4111",
|
|
195
|
+
headers: {
|
|
196
|
+
Authorization: `Bearer ${token}`
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const weatherAgent = mastra.getAgent("weatherAgent");
|
|
201
|
+
const response = await weatherAgent.generate("What's the weather like in New York");
|
|
202
|
+
|
|
203
|
+
setResult(response.text);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<div>
|
|
208
|
+
<button onClick={callMastraApi}>
|
|
209
|
+
Test Mastra API
|
|
210
|
+
</button>
|
|
211
|
+
|
|
212
|
+
{result && (
|
|
213
|
+
<div className="result">
|
|
214
|
+
<h6>Result:</h6>
|
|
215
|
+
<pre>{result}</pre>
|
|
216
|
+
</div>
|
|
217
|
+
)}
|
|
218
|
+
</div>
|
|
219
|
+
);
|
|
220
|
+
};
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
**curl:**
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
228
|
+
-H "Content-Type: application/json" \
|
|
229
|
+
-H "Authorization: Bearer <your-auth0-access-token>" \
|
|
230
|
+
-d '{
|
|
231
|
+
"messages": "Weather in London"
|
|
232
|
+
}'
|
|
233
|
+
```
|