Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: add Realtime custom token instruction (#12175)

Wen Bo Xie1ad783313229
diff --git a/apps/docs/pages/guides/realtime/postgres-changes.mdx b/apps/docs/pages/guides/realtime/postgres-changes.mdx
index 205af17987c..0e4e9638855 100644
--- a/apps/docs/pages/guides/realtime/postgres-changes.mdx
+++ b/apps/docs/pages/guides/realtime/postgres-changes.mdx
@@ -19,9 +19,8 @@ GRANT SELECT ON "private_schema"."table" TO authenticated;
```
<Admonition type="caution">
-
-We strongly encourage you to enable RLS and create policies for tables in private schemas. Otherwise, any role you grant access to will have unfettered read access to the table.
-
+ We strongly encourage you to enable RLS and create policies for tables in private schemas.
+ Otherwise, any role you grant access to will have unfettered read access to the table.
</Admonition>
## Replication Setup
@@ -142,7 +141,8 @@ const channel = supabase
},
(payload) => console.log(payload)
)
- .on('postgres_changes',
+ .on(
+ 'postgres_changes',
{
event: 'INSERT',
schema: 'public',
@@ -153,6 +153,38 @@ const channel = supabase
.subscribe()
```
+## Custom Tokens
+
+You may choose to sign your own tokens to customize claims that can be checked in your RLS policies.
+In order for this to work you must pass `apikey` in both Realtime's `headers` and `params` when creating the client.
+
+The `apikey` in `headers` must be either the `anon` or `service_role` token that Supabase signed.
+This will authenticate your request in the API gateway.
+
+<Admonition type="caution">
+ Do not expose the `service_role` token on the client because the role is authorized to bypass
+ row-level security.
+</Admonition>
+
+The `apikey` in `params` can be your custom token signed with the JWT secret of your Supabase project.
+This is forwarded to the Realtime server and it will verify your custom token and use its claims to authorize database changes
+when RLS is enabled.
+
+```js
+const { createClient } = require('@supabase/supabase-js')
+
+const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY, {
+ realtime: {
+ headers: {
+ apikey: 'supabase-signed-token',
+ },
+ params: {
+ apikey: 'your-custom-token',
+ },
+ },
+})
+```
+
export const Page = ({ children }) => <Layout meta={meta} children={children} />
export default Page