You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -104,68 +104,178 @@ The description should be a short description of what the code is doing and be u
104
104
105
105
You will need to setup the database schema.
106
106
107
+
### Row Level Security (RLS)
108
+
109
+
**⚠️ SECURITY WARNING: ALWAYS ENABLE RLS ON ALL TABLES**
110
+
111
+
Row Level Security (RLS) is MANDATORY for all tables in Supabase. Without RLS policies, ANY user can read, insert, update, or delete ANY data in your database, creating massive security vulnerabilities.
112
+
113
+
#### RLS Best Practices (REQUIRED):
114
+
115
+
1. **Enable RLS on Every Table:**
116
+
<dyad-execute-sql description="Enable RLS on table">
117
+
ALTER TABLE table_name ENABLE ROW LEVEL SECURITY;
118
+
</dyad-execute-sql>
119
+
120
+
2. **Create Appropriate Policies for Each Operation:**
121
+
- SELECT policies (who can read data)
122
+
- INSERT policies (who can create data)
123
+
- UPDATE policies (who can modify data)
124
+
- DELETE policies (who can remove data)
125
+
126
+
3. **Common RLS Policy Patterns:**
127
+
128
+
**Public Read Access:** (ONLY USE THIS IF SPECIFICALLY REQUESTED)
129
+
<dyad-execute-sql description="Create public read access policy">
130
+
CREATE POLICY "Public read access" ON table_name FOR SELECT USING (true);
131
+
</dyad-execute-sql>
132
+
133
+
**User-specific Data Access:**
134
+
<dyad-execute-sql description="Create user-specific data access policy">
135
+
CREATE POLICY "Users can only see their own data" ON table_name
136
+
FOR SELECT TO authenticated USING (auth.uid() = user_id);
137
+
138
+
CREATE POLICY "Users can only insert their own data" ON table_name
139
+
FOR INSERT TO authenticated WITH CHECK (auth.uid() = user_id);
140
+
141
+
CREATE POLICY "Users can only update their own data" ON table_name
142
+
FOR UPDATE TO authenticated USING (auth.uid() = user_id);
143
+
144
+
CREATE POLICY "Users can only delete their own data" ON table_name
145
+
FOR DELETE TO authenticated USING (auth.uid() = user_id);
146
+
</dyad-execute-sql>
147
+
148
+
#### RLS Policy Creation Template:
149
+
150
+
When creating any table, ALWAYS follow this pattern:
151
+
152
+
<dyad-execute-sql description="Create table">
153
+
-- Create table
154
+
CREATE TABLE table_name (
155
+
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
156
+
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
157
+
-- other columns
158
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
159
+
);
160
+
161
+
-- Enable RLS (REQUIRED)
162
+
ALTER TABLE table_name ENABLE ROW LEVEL SECURITY;
163
+
164
+
-- Create policies for each operation needed
165
+
CREATE POLICY "policy_name_select" ON table_name
166
+
FOR SELECT TO authenticated USING (auth.uid() = user_id);
167
+
168
+
CREATE POLICY "policy_name_insert" ON table_name
169
+
FOR INSERT TO authenticated WITH CHECK (auth.uid() = user_id);
170
+
171
+
CREATE POLICY "policy_name_update" ON table_name
172
+
FOR UPDATE TO authenticated USING (auth.uid() = user_id);
173
+
174
+
CREATE POLICY "policy_name_delete" ON table_name
175
+
FOR DELETE TO authenticated USING (auth.uid() = user_id);
176
+
</dyad-execute-sql>
177
+
178
+
**REMINDER: If you create a table without proper RLS policies, any user can access, modify, or delete ALL data in that table.**
179
+
180
+
#### Security Checklist for Every Database Operation:
181
+
182
+
Before creating any table or database schema, verify:
183
+
184
+
- ✅ RLS is enabled on the table
185
+
- ✅ Appropriate SELECT policies are defined
186
+
- ✅ Appropriate INSERT policies are defined
187
+
- ✅ Appropriate UPDATE policies are defined
188
+
- ✅ Appropriate DELETE policies are defined
189
+
- ✅ Policies follow the principle of least privilege
190
+
- ✅ User can only access their own data (unless public access is specifically required)
191
+
- ✅ All user-specific policies include \`TO authenticated\` for additional security
192
+
193
+
**Remember: Without proper RLS policies, your database is completely exposed to unauthorized access.**
194
+
107
195
## Creating User Profiles
108
196
109
197
If the user wants to create a user profile, use the following code:
110
198
111
-
### Create profiles table in public schema
199
+
### Create profiles table in public schema with proper RLS
112
200
113
-
<dyad-execute-sql description="Create profiles table in public schema">
201
+
<dyad-execute-sql description="Create profiles table with proper RLS security">
202
+
-- Create profiles table
114
203
CREATE TABLE public.profiles (
115
-
id UUID NOT NULL REFERENCES auth.users ON DELETE CASCADE,
204
+
id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
116
205
first_name TEXT,
117
206
last_name TEXT,
207
+
avatar_url TEXT,
208
+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
118
209
PRIMARY KEY (id)
119
210
);
120
211
121
-
alter table public.profiles enable row level security;
212
+
-- Enable RLS (REQUIRED for security)
213
+
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
214
+
215
+
-- Create secure policies for each operation
216
+
CREATE POLICY "profiles_select_policy" ON public.profiles
217
+
FOR SELECT TO authenticated USING (auth.uid() = id);
122
218
123
-
create policy "Public profiles are viewable by everyone." on profiles for select using ( true );
219
+
CREATE POLICY "profiles_insert_policy" ON public.profiles
220
+
FOR INSERT TO authenticated WITH CHECK (auth.uid() = id);
124
221
125
-
create policy "Users can insert their own profile." on profiles for insert with check ( auth.uid() = id );
222
+
CREATE POLICY "profiles_update_policy" ON public.profiles
223
+
FOR UPDATE TO authenticated USING (auth.uid() = id);
126
224
127
-
create policy "Users can update own profile." on profiles for update using ( auth.uid() = id );
225
+
CREATE POLICY "profiles_delete_policy" ON public.profiles
226
+
FOR DELETE TO authenticated USING (auth.uid() = id);
227
+
</dyad-execute-sql>
228
+
229
+
**SECURITY NOTE:** These policies ensure users can only access, modify, and delete their own profile data. If you need public profile visibility (e.g., for a social app), add an additional public read policy only if specifically required:
230
+
231
+
<dyad-execute-sql description="Optional: Add public read access (only if needed)">
232
+
-- ONLY add this policy if public profile viewing is specifically required
233
+
CREATE POLICY "profiles_public_read_policy" ON public.profiles
234
+
FOR SELECT USING (true);
128
235
</dyad-execute-sql>
129
236
130
237
**IMPORTANT:** For security, Auth schema isn't exposed in the API. Create user tables in public schema to access user data via API.
131
238
132
239
**CAUTION:** Only use primary keys as foreign key references for Supabase-managed schemas like auth.users. While PostgreSQL allows referencing columns backed by unique indexes, primary keys are guaranteed not to change.
133
240
134
-
135
241
## Auto-Update Profiles on Signup
136
242
137
-
138
243
### Function to insert profile when user signs up
139
244
140
245
<dyad-execute-sql description="Create function to insert profile when user signs up">
141
-
CREATE FUNCTION public.handle_new_user()
246
+
CREATE OR REPLACE FUNCTION public.handle_new_user()
142
247
RETURNS TRIGGER
143
248
LANGUAGE PLPGSQL
144
249
SECURITY DEFINER SET search_path = ''
145
250
AS $$
146
251
BEGIN
147
252
INSERT INTO public.profiles (id, first_name, last_name)
0 commit comments