Skip to content

Commit d17fe9d

Browse files
committed
Reordered react hooks + frontend sections
1 parent 0d136a3 commit d17fe9d

18 files changed

+1458
-732
lines changed

docs/docs.json

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,33 @@
112112
]
113113
},
114114
{
115-
"group": "Frontend usage",
115+
"group": "Realtime",
116116
"pages": [
117-
"frontend/overview",
117+
"realtime/overview",
118+
"realtime/how-it-works",
119+
"realtime/run-object",
120+
"realtime/auth",
121+
{
122+
"group": "React hooks (frontend)",
123+
"pages": [
124+
"realtime/react-hooks/overview",
125+
"realtime/react-hooks/realtime",
126+
"realtime/react-hooks/metadata",
127+
"realtime/react-hooks/streams",
128+
"realtime/react-hooks/triggering"
129+
]
130+
},
118131
{
119-
"group": "React hooks",
132+
"group": "Backend",
120133
"pages": [
121-
"frontend/react-hooks/overview",
122-
"frontend/react-hooks/realtime",
123-
"frontend/react-hooks/triggering"
134+
"realtime/backend/overview",
135+
"realtime/backend/subscribe",
136+
"realtime/backend/metadata",
137+
"realtime/backend/streams"
124138
]
125139
}
126140
]
127141
},
128-
{
129-
"group": "Realtime API",
130-
"pages": [
131-
"realtime/overview",
132-
"realtime/streams",
133-
"realtime/react-hooks",
134-
"realtime/subscribe-to-run",
135-
"realtime/subscribe-to-runs-with-tag",
136-
"realtime/subscribe-to-batch"
137-
]
138-
},
139142
{
140143
"group": "CLI",
141144
"pages": [
@@ -570,7 +573,47 @@
570573
},
571574
{
572575
"source": "/frontend/react-hooks",
573-
"destination": "/frontend/react-hooks/overview"
576+
"destination": "/realtime/react-hooks/overview"
577+
},
578+
{
579+
"source": "/frontend/overview",
580+
"destination": "/realtime/auth"
581+
},
582+
{
583+
"source": "/frontend/react-hooks/overview",
584+
"destination": "/realtime/react-hooks/overview"
585+
},
586+
{
587+
"source": "/frontend/react-hooks/realtime",
588+
"destination": "/realtime/react-hooks/realtime"
589+
},
590+
{
591+
"source": "/frontend/react-hooks/triggering",
592+
"destination": "/realtime/react-hooks/triggering"
593+
},
594+
{
595+
"source": "/realtime/backend",
596+
"destination": "/realtime/backend/overview"
597+
},
598+
{
599+
"source": "/realtime/streams",
600+
"destination": "/realtime/backend/streams"
601+
},
602+
{
603+
"source": "/realtime/react-hooks",
604+
"destination": "/realtime/react-hooks/overview"
605+
},
606+
{
607+
"source": "/realtime/subscribe-to-run",
608+
"destination": "/realtime/backend/subscribe"
609+
},
610+
{
611+
"source": "/realtime/subscribe-to-runs-with-tag",
612+
"destination": "/realtime/backend/subscribe"
613+
},
614+
{
615+
"source": "/realtime/subscribe-to-batch",
616+
"destination": "/realtime/backend/subscribe"
574617
},
575618
{
576619
"source": "/management/projects/runs",
Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
2-
title: Overview & Authentication
3-
sidebarTitle: Overview & Auth
4-
description: Using the Trigger.dev SDK from your frontend application.
2+
title: Authentication
3+
sidebarTitle: Authentication
4+
description: Authenticating real-time API requests with Public Access Tokens
55
---
66

7-
You can use our [React hooks](/frontend/react-hooks) in your frontend application to interact with the Trigger.dev API. This guide will show you how to generate Public Access Tokens that can be used to authenticate your requests.
7+
To use the Realtime API, you need to authenticate your requests with Public Access Tokens. These tokens provide secure, scoped access to your runs and can be used in both frontend and backend applications.
88

9-
## Authentication
9+
## Creating Public Access Tokens
1010

11-
To create a Public Access Token, you can use the `auth.createPublicToken` function in your **backend** code:
11+
You can create a Public Access Token using the `auth.createPublicToken` function in your **backend** code:
1212

1313
```tsx
1414
import { auth } from "@trigger.dev/sdk/v3";
1515

1616
const publicToken = await auth.createPublicToken(); // 👈 this public access token has no permissions, so is pretty useless!
1717
```
1818

19-
### Scopes
19+
## Scopes
2020

2121
By default a Public Access Token has no permissions. You must specify the scopes you need when creating a Public Access Token:
2222

@@ -103,7 +103,7 @@ const publicToken = await auth.createPublicToken({
103103
});
104104
```
105105

106-
### Expiration
106+
## Expiration
107107

108108
By default, Public Access Token's expire after 15 minutes. You can specify a different expiration time when creating a Public Access Token:
109109

@@ -166,6 +166,78 @@ const handle = await tasks.batchTrigger("my-task", [
166166
console.log(handle.publicAccessToken);
167167
```
168168

169-
## Usage
169+
## Trigger tokens
170170

171-
To learn how to use these Public Access Tokens, see our [React hooks](/frontend/react-hooks) guide.
171+
For triggering tasks from your frontend, you need special "trigger" tokens. These tokens can only be used once to trigger a task and are more secure than regular Public Access Tokens.
172+
173+
```ts
174+
import { auth } from "@trigger.dev/sdk/v3";
175+
176+
// Somewhere in your backend code
177+
const triggerToken = await auth.createTriggerPublicToken("my-task");
178+
```
179+
180+
These tokens also expire, with the default expiration time being 15 minutes. You can specify a custom expiration time:
181+
182+
```ts
183+
import { auth } from "@trigger.dev/sdk/v3";
184+
185+
// Somewhere in your backend code
186+
const triggerToken = await auth.createTriggerPublicToken("my-task", {
187+
expirationTime: "24hr",
188+
});
189+
```
190+
191+
You can pass multiple tasks to create a token that can trigger multiple tasks:
192+
193+
```ts
194+
import { auth } from "@trigger.dev/sdk/v3";
195+
196+
// Somewhere in your backend code
197+
const triggerToken = await auth.createTriggerPublicToken(["my-task-1", "my-task-2"]);
198+
```
199+
200+
You can also create tokens that can be used multiple times:
201+
202+
```ts
203+
import { auth } from "@trigger.dev/sdk/v3";
204+
205+
// Somewhere in your backend code
206+
const triggerToken = await auth.createTriggerPublicToken("my-task", {
207+
multipleUse: true, // ❌ Use this with caution!
208+
});
209+
```
210+
211+
## Using tokens
212+
213+
Once you have a Public Access Token, you can use it to authenticate requests to the Realtime API:
214+
215+
### Backend usage
216+
217+
```ts
218+
import { runs } from "@trigger.dev/sdk/v3";
219+
220+
// Using the token with backend functions
221+
for await (const run of runs.subscribeToRun("run_1234", {
222+
accessToken: publicToken,
223+
})) {
224+
console.log(run);
225+
}
226+
```
227+
228+
### Frontend usage
229+
230+
```tsx
231+
import { useRealtimeRun } from "@trigger.dev/react-hooks";
232+
233+
export function MyComponent({ runId, publicAccessToken }) {
234+
const { run, error } = useRealtimeRun(runId, {
235+
accessToken: publicAccessToken,
236+
});
237+
238+
if (error) return <div>Error: {error.message}</div>;
239+
return <div>Run: {run?.id}</div>;
240+
}
241+
```
242+
243+
See our [React hooks documentation](/realtime/react-hooks) for more details on using hooks in your frontend application.

0 commit comments

Comments
 (0)