Skip to content

Commit fc7e196

Browse files
authored
Added supabase auth page and updated examples (triggerdotdev#1838)
* Added supabase auth page * Added auth info snippet * Added snippet and updated examples
1 parent 49c43a1 commit fc7e196

8 files changed

+138
-23
lines changed

docs/docs.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@
281281
"pages": [
282282
"guides/frameworks/supabase-guides-overview",
283283
"guides/frameworks/supabase-edge-functions-basic",
284-
"guides/frameworks/supabase-edge-functions-database-webhooks"
284+
"guides/frameworks/supabase-edge-functions-database-webhooks",
285+
"guides/frameworks/supabase-authentication"
285286
]
286287
},
287288
{

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

+25-14
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

+3
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:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: "Authenticating Supabase tasks: JWTs and service roles"
3+
sidebarTitle: "Supabase authentication"
4+
description: "Learn how to authenticate Supabase tasks using JWTs for Row Level Security (RLS) or service role keys for admin access."
5+
---
6+
7+
import SupabaseDocsCards from "/snippets/supabase-docs-cards.mdx";
8+
9+
There are two ways to authenticate your Supabase client in Trigger.dev tasks:
10+
11+
### 1. Using JWT Authentication (Recommended for User-Specific Operations)
12+
13+
A JWT (JSON Web Token) is a string-formatted data container that typically stores user identity and permissions data. Row Level Security policies are based on the information present in JWTs. Supabase JWT docs can be found [here](https://supabase.com/docs/guides/auth/jwts).
14+
15+
To use JWTs with Supabase, you'll need to add the `SUPABASE_JWT_SECRET` environment variable in your project. This secret is used to sign the JWTs. This can be found in your Supabase project settings under `Data API`.
16+
17+
This example code shows how to create a JWT token for a user and initialize a Supabase client with that token for authentication, allowing the task to perform database operations as that specific user. You can adapt this code to fit your own use case.
18+
19+
```ts
20+
21+
// The rest of your task code
22+
async run(payload: { user_id: string }) {
23+
const { user_id } = payload;
24+
25+
// Optional error handling
26+
const jwtSecret = process.env.SUPABASE_JWT_SECRET;
27+
if (!jwtSecret) {
28+
throw new Error(
29+
"SUPABASE_JWT_SECRET is not defined in environment variables"
30+
);
31+
}
32+
33+
// Create a JWT token for the user that expires in 1 hour
34+
const token = jwt.sign({ sub: user_id }, jwtSecret, { expiresIn: "1h" });
35+
36+
// Initialize the Supabase client with the JWT token
37+
const supabase = createClient(
38+
// These details can be found in your Supabase project settings under `Data API`
39+
process.env.SUPABASE_URL as string,
40+
process.env.SUPABASE_ANON_KEY as string,
41+
{
42+
global: {
43+
headers: {
44+
Authorization: `Bearer ${token}`,
45+
},
46+
},
47+
}
48+
);
49+
// The rest of your task code
50+
```
51+
52+
Using JWTs to authenticate Supabase operations is more secure than using service role keys because it respects Row Level Security policies, maintains user-specific audit trails, and follows the principle of least privileged access.
53+
54+
### 2. Using Service Role Key (For Admin-Level Access)
55+
56+
<Warning>
57+
The service role key has unlimited access and bypasses all security checks. Only use it when you
58+
need admin-level privileges, and never expose it client-side.
59+
</Warning>
60+
61+
This example code creates a Supabase client with admin-level privileges using a service role key, bypassing all Row Level Security policies to allow unrestricted database access.
62+
63+
```ts
64+
// Create a single Supabase client for interacting with your database
65+
// 'Database' supplies the type definitions to supabase-js
66+
const supabase = createClient<Database>(
67+
// These details can be found in your Supabase project settings under `API`
68+
process.env.SUPABASE_PROJECT_URL as string, // e.g. https://abc123.supabase.co - replace 'abc123' with your project ID
69+
process.env.SUPABASE_SERVICE_ROLE_KEY as string // Your service role secret key
70+
);
71+
72+
// Your task
73+
```
74+
75+
<SupabaseDocsCards />

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

+7-4
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

+11-3
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-auth-info.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Note>
2+
To learn more about how to properly configure Supabase auth for Trigger.dev tasks, please refer to
3+
our [Supabase Authentication guide](/guides/frameworks/supabase-authentication). It demonstrates
4+
how to use JWT authentication for user-specific operations or your service role key for
5+
admin-level access.
6+
</Note>

docs/snippets/supabase-docs-cards.mdx

+9-1
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)