Skip to content

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

docs: Adds the schema for people to get an idea of RLS

Paul Copplestone55a0aa5b7fa7
diff --git a/examples/slack-clone/README.md b/examples/slack-clone/README.md
index 05e48237c7a..fb732cc93eb 100644
--- a/examples/slack-clone/README.md
+++ b/examples/slack-clone/README.md
@@ -40,6 +40,54 @@ You will be asked for a `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_KEY
## For developers
+### Postgres Row level security
+
+This project uses very high-level Authorization using Postgres' Role Level Security.
+When you start a Postgres database on Supabase, we populate it with an `auth` schema, and some helper functions.
+When a user logs in, they are issued a JWT with the role `authenticated` and thier UUID.
+We can use these details to provide fine-grained control over what each user can and cannot do.
+
+This is a trimmed-down schema, with the policies:
+
+```sql
+-- USER PROFILES
+CREATE TYPE public.user_status AS ENUM ('ONLINE', 'OFFLINE');
+CREATE TABLE public.users (
+ id uuid NOT NULL PRIMARY KEY, -- UUID from auth.users (Supabase)
+ username text,
+ status user_status DEFAULT 'OFFLINE'::public.user_status
+);
+ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;
+CREATE POLICY "Allow logged-in read access" on public.users FOR SELECT USING ( auth.role() = 'authenticated' );
+CREATE POLICY "Allow individual insert access" on public.users FOR INSERT WITH CHECK ( auth.uid() = id );
+CREATE POLICY "Allow individual update access" on public.users FOR UPDATE USING ( auth.uid() = id );
+
+-- CHANNELS
+CREATE TABLE public.channels (
+ id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ slug text NOT NULL UNIQUE
+);
+ALTER TABLE public.channels ENABLE ROW LEVEL SECURITY;
+CREATE POLICY "Allow logged-in full access" on public.channels FOR ALL USING ( auth.role() = 'authenticated' );
+
+-- MESSAGES
+CREATE TABLE public.messages (
+ id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ message text,
+ user_id uuid REFERENCES public.users NOT NULL,
+ channel_id bigint REFERENCES public.channels NOT NULL
+);
+ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
+CREATE POLICY "Allow logged-in read access" on public.messages USING ( auth.role() = 'authenticated' );
+CREATE POLICY "Allow individual insert access" on public.messages FOR INSERT WITH CHECK ( auth.uid() = user_id );
+CREATE POLICY "Allow individual update access" on public.messages FOR UPDATE USING ( auth.uid() = user_id );
+```
+
+
+### Run locally
+
If you want to run this without deploying:
**Clone this folder**
@@ -63,3 +111,5 @@ npm run dev # start Next.js
```
Visit http://localhost:3000 and start slacking! Open in two tabs to see everything getting updated in realtime
+
+