Skip to content

Commit 0058c5f

Browse files
committed
Added snippet and updated examples
1 parent ea90268 commit 0058c5f

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

docs/guides/examples/supabase-database-operations.mdx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: "These examples demonstrate how to run basic CRUD operations on a t
55
---
66

77
import SupabaseDocsCards from "/snippets/supabase-docs-cards.mdx";
8+
import SupabaseAuthInfo from "/snippets/supabase-auth-info.mdx";
89

910
## Add a new user to a table in a Supabase database
1011

@@ -27,22 +28,37 @@ This is a basic task which inserts a new row into a table from a Trigger.dev tas
2728
```ts trigger/supabase-database-insert.ts
2829
import { createClient } from "@supabase/supabase-js";
2930
import { task } from "@trigger.dev/sdk/v3";
31+
import jwt from "jsonwebtoken";
3032
// Generate the Typescript types using the Supabase CLI: https://supabase.com/docs/guides/api/rest/generating-types
3133
import { Database } from "database.types";
3234

33-
// Create a single Supabase client for interacting with your database
34-
// 'Database' supplies the type definitions to supabase-js
35-
const supabase = createClient<Database>(
36-
// These details can be found in your Supabase project settings under `API`
37-
process.env.SUPABASE_PROJECT_URL as string, // e.g. https://abc123.supabase.co - replace 'abc123' with your project ID
38-
process.env.SUPABASE_SERVICE_ROLE_KEY as string // Your service role secret key
39-
);
40-
4135
export const supabaseDatabaseInsert = task({
4236
id: "add-new-user",
4337
run: async (payload: { userId: string }) => {
4438
const { userId } = payload;
4539

40+
// Get JWT secret from env vars
41+
const jwtSecret = process.env.SUPABASE_JWT_SECRET;
42+
if (!jwtSecret) {
43+
throw new Error("SUPABASE_JWT_SECRET is not defined in environment variables");
44+
}
45+
46+
// Create JWT token for the user
47+
const token = jwt.sign({ sub: userId }, jwtSecret, { expiresIn: "1h" });
48+
49+
// Initialize Supabase client with JWT
50+
const supabase = createClient<Database>(
51+
process.env.SUPABASE_URL as string,
52+
process.env.SUPABASE_ANON_KEY as string,
53+
{
54+
global: {
55+
headers: {
56+
Authorization: `Bearer ${token}`,
57+
},
58+
},
59+
}
60+
);
61+
4662
// Insert a new row into the user_subscriptions table with the provided userId
4763
const { error } = await supabase.from("user_subscriptions").insert({
4864
user_id: userId,
@@ -60,12 +76,7 @@ export const supabaseDatabaseInsert = task({
6076
});
6177
```
6278

63-
<Note>
64-
This task uses your service role secret key to bypass Row Level Security. There are different ways
65-
of configuring your [RLS
66-
policies](https://supabase.com/docs/guides/database/postgres/row-level-security), so always make
67-
sure you have the correct permissions set up for your project.
68-
</Note>
79+
<SupabaseAuthInfo />
6980

7081
### Testing your task
7182

docs/guides/examples/supabase-storage-upload.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: "This example demonstrates how to upload files to Supabase Storage
55
---
66

77
import SupabaseDocsCards from "/snippets/supabase-docs-cards.mdx";
8+
import SupabaseAuthInfo from "/snippets/supabase-auth-info.mdx";
89

910
## Overview
1011

@@ -137,6 +138,8 @@ export const supabaseStorageUploadS3 = task({
137138
});
138139
```
139140

141+
<SupabaseAuthInfo />
142+
140143
### Testing your task
141144

142145
To test this task in the dashboard, you can use the following payload:

docs/guides/frameworks/supabase-edge-functions-basic.mdx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import NextjsTroubleshootingButtonSyntax from "/snippets/nextjs-button-syntax.md
1717
import WorkerFailedToStartWhenRunningDevCommand from "/snippets/worker-failed-to-start.mdx";
1818
import SupabaseDocsCards from "/snippets/supabase-docs-cards.mdx";
1919

20+
import SupabaseAuthInfo from "/snippets/supabase-auth-info.mdx";
21+
2022
## Overview
2123

2224
Supabase edge functions allow you to trigger tasks either when an event is sent from a third party (e.g. when a new Stripe payment is processed, when a new user signs up to a service, etc), or when there are any changes or updates to your Supabase database.
@@ -109,11 +111,12 @@ You can now deploy your edge function with the following command in your termina
109111
supabase functions deploy edge-function-trigger --no-verify-jwt
110112
```
111113

112-
<Note>
114+
<Warning>
113115
`--no-verify-jwt` removes the JSON Web Tokens requirement from the authorization header. By
114-
default this should be on, but it is not required for this example. Learn more about JWTs
115-
[here](https://supabase.com/docs/guides/auth/jwts).
116-
</Note>
116+
default this should be on, but it is not strictly required for this hello world example.
117+
</Warning>
118+
119+
<SupabaseAuthInfo />
117120

118121
Follow the CLI instructions and once complete you should now see your new edge function deployment in your Supabase edge functions dashboard.
119122

docs/guides/frameworks/supabase-edge-functions-database-webhooks.mdx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import NextjsTroubleshootingMissingApiKey from "/snippets/nextjs-missing-api-key
1212
import NextjsTroubleshootingButtonSyntax from "/snippets/nextjs-button-syntax.mdx";
1313
import WorkerFailedToStartWhenRunningDevCommand from "/snippets/worker-failed-to-start.mdx";
1414
import SupabaseDocsCards from "/snippets/supabase-docs-cards.mdx";
15+
import SupabaseAuthInfo from "/snippets/supabase-auth-info.mdx";
1516

1617
## Overview
1718

@@ -136,6 +137,12 @@ yarn install @deepgram/sdk @supabase/supabase-js fluent-ffmpeg
136137

137138
These dependencies will allow you to interact with the Deepgram and Supabase APIs and extract audio from a video using FFmpeg.
138139

140+
<Warning>
141+
When updating your tables from a Trigger.dev task which has been triggered by a database change,
142+
be extremely careful to not cause an infinite loop. Ensure you have the correct conditions in
143+
place to prevent this.
144+
</Warning>
145+
139146
```ts /trigger/videoProcessAndUpdate.ts
140147
// Install any missing dependencies below
141148
import { createClient as createDeepgramClient } from "@deepgram/sdk";
@@ -235,11 +242,12 @@ export const videoProcessAndUpdate = task({
235242
```
236243

237244
<Warning>
238-
When updating your tables from a Trigger.dev task which has been triggered by a database change,
239-
be extremely careful to not cause an infinite loop. Ensure you have the correct conditions in
240-
place to prevent this.
245+
This task uses your service role secret key to bypass Row Level Security. This is not recommended
246+
for production use as it has unlimited access and bypasses all security checks.
241247
</Warning>
242248

249+
<SupabaseAuthInfo />
250+
243251
### Adding the FFmpeg build extension
244252

245253
Before you can deploy the task, you'll need to add the FFmpeg build extension to your `trigger.config.ts` file.

docs/snippets/supabase-docs-cards.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Full walkthrough guides from development to deployment
44

5-
<CardGroup cols={2}>
5+
<CardGroup cols={1}>
66
<Card
77
title="Edge function hello world guide"
88
icon="book"
@@ -17,6 +17,14 @@
1717
>
1818
Learn how to trigger a task from a Supabase edge function when an event occurs in your database.
1919
</Card>
20+
<Card
21+
title="Supabase authentication guide"
22+
icon="book"
23+
href="/guides/frameworks/supabase-authentication"
24+
>
25+
Learn how to authenticate Supabase tasks using JWTs for Row Level Security (RLS) or service role
26+
keys for admin access.
27+
</Card>
2028
</CardGroup>
2129

2230
### Task examples with code you can copy and paste

0 commit comments

Comments
 (0)