mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
Style pass only, per the `edit-the-docs` skill. No section moved, and no behavior claim changed. - The 1.x/2.x compatibility fact was stated three times on the frameworks page. It is stated once now, in the Versions admonition, which also stops restating the removal the body already covers. - Long sentences split: the removal clause packed three versions and a date into one, and the `JWTClaims` index-signature paragraph ran 37 words. - Negative and mannered constructions rewritten positively: "Do not rely on the compiler to catch" and "that is the case to worry about". - "compiles clean" is an idiom that localizes badly, and `runtime` was the wrong term for the moment of execution. Now "compiles without error" and "at run time". - The Edge Functions bridge setup was three actions in one prose sentence. It is a numbered procedure now, which is also what an agent can follow. - Nuxt tutorial: the anonymous-mode paragraph carried a procedure, a process, a warning and navigation at once, with a silent-auth-bypass warning buried mid-paragraph. Split, with the warning in a caution admonition. - Smaller fixes: Oxford comma, a reference page called a guide, a 42-word sentence hiding its performance claim in parentheses, the unintroduced `RLS` abbreviation, "See X for Y" link order, and an external GitHub Pages link left pointing away from the docs reference. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
99 lines
3.3 KiB
Plaintext
99 lines
3.3 KiB
Plaintext
---
|
|
id: usage-examples
|
|
title: Usage examples
|
|
---
|
|
|
|
Each example is a Fetch handler. Deno, Bun, and Supabase Edge Functions run the `export default { fetch }` shape as is. Cloudflare Workers also need the `nodejs_compat` flag or the `env` option, so `withSupabase` can read your project's URL and keys. On Node, use a framework bridge or compose the primitives yourself.
|
|
|
|
### Protect an endpoint with a user JWT
|
|
|
|
<RefSubLayout.EducationRow>
|
|
<RefSubLayout.Details>
|
|
|
|
`withSupabase` wraps your handler. With `auth: 'user'`, it checks the caller's JWT before your handler runs. A request without valid credentials gets a JSON error response, and your handler never runs.
|
|
|
|
`withSupabase` also answers every `OPTIONS` request with `204` and adds CORS headers to every response. Set `cors: 'disabled'` when your framework handles CORS.
|
|
|
|
`ctx.supabase` is scoped to the caller, so Row Level Security policies apply. `ctx.supabaseAdmin` bypasses RLS. Use it only for work that needs full database access.
|
|
|
|
</RefSubLayout.Details>
|
|
|
|
<RefSubLayout.Examples>
|
|
|
|
```ts
|
|
import { withSupabase } from '@supabase/server'
|
|
|
|
export default {
|
|
fetch: withSupabase({ auth: 'user' }, async (_req, ctx) => {
|
|
const { data } = await ctx.supabase.from('todos').select()
|
|
return Response.json(data)
|
|
}),
|
|
}
|
|
```
|
|
|
|
</RefSubLayout.Examples>
|
|
</RefSubLayout.EducationRow>
|
|
|
|
### Serve a public endpoint
|
|
|
|
<RefSubLayout.EducationRow>
|
|
<RefSubLayout.Details>
|
|
|
|
`auth: 'none'` lets every request through. `ctx.userClaims` is `null`, and `ctx.supabase` runs as an anonymous client.
|
|
|
|
Supabase Edge Functions check for a valid JWT on every request by default. For a function that uses `auth: 'none'`, `auth: 'publishable'`, or `auth: 'secret'`, turn that check off in `supabase/config.toml`. The `auth` setting on `withSupabase` then decides who gets in.
|
|
|
|
</RefSubLayout.Details>
|
|
|
|
<RefSubLayout.Examples>
|
|
|
|
```ts
|
|
import { withSupabase } from '@supabase/server'
|
|
|
|
export default {
|
|
fetch: withSupabase({ auth: 'none' }, async () => {
|
|
return Response.json({ status: 'ok', time: new Date().toISOString() })
|
|
}),
|
|
}
|
|
```
|
|
|
|
```toml supabase/config.toml
|
|
[functions.my-function]
|
|
verify_jwt = false
|
|
```
|
|
|
|
</RefSubLayout.Examples>
|
|
</RefSubLayout.EducationRow>
|
|
|
|
### Build the context yourself
|
|
|
|
<RefSubLayout.EducationRow>
|
|
<RefSubLayout.Details>
|
|
|
|
`createSupabaseContext` runs the same auth checks as `withSupabase` but returns `{ data, error }` instead of a response. Use it inside a framework route handler, or anywhere you want to shape the error response yourself.
|
|
|
|
On success, `data` is the same `SupabaseContext` that `withSupabase` passes to your handler. On failure, `error.status` holds the HTTP status to send. `createSupabaseContext` does not handle CORS.
|
|
|
|
</RefSubLayout.Details>
|
|
|
|
<RefSubLayout.Examples>
|
|
|
|
```ts
|
|
import { createSupabaseContext } from '@supabase/server'
|
|
|
|
export default {
|
|
fetch: async (req: Request) => {
|
|
const { data: ctx, error } = await createSupabaseContext(req, { auth: 'user' })
|
|
if (error) {
|
|
return Response.json(error.toJSON(), { status: error.status })
|
|
}
|
|
|
|
const { data } = await ctx.supabase.from('todos').select()
|
|
return Response.json(data)
|
|
},
|
|
}
|
|
```
|
|
|
|
</RefSubLayout.Examples>
|
|
</RefSubLayout.EducationRow>
|