-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-storage-policies.sql
More file actions
114 lines (100 loc) · 3.84 KB
/
supabase-storage-policies.sql
File metadata and controls
114 lines (100 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- =====================================================
-- Supabase Storage Policies for Avatars Bucket
-- =====================================================
-- Run this in your Supabase SQL Editor after creating
-- the "avatars" bucket in Storage
-- =====================================================
-- Enable RLS on storage.objects (if not already enabled)
-- Note: This is usually enabled by default, but we include it for safety
-- Policy: Allow authenticated users to upload their own avatars
-- Users can only upload files that start with their user ID
CREATE POLICY "Users can upload their own avatars"
ON storage.objects
FOR INSERT
TO authenticated
WITH CHECK (
bucket_id = 'avatars' AND
(storage.foldername(name))[1] = auth.uid()::text OR
name LIKE auth.uid()::text || '-%'
);
-- Policy: Allow authenticated users to update their own avatars
CREATE POLICY "Users can update their own avatars"
ON storage.objects
FOR UPDATE
TO authenticated
USING (
bucket_id = 'avatars' AND
(storage.foldername(name))[1] = auth.uid()::text OR
name LIKE auth.uid()::text || '-%'
)
WITH CHECK (
bucket_id = 'avatars' AND
(storage.foldername(name))[1] = auth.uid()::text OR
name LIKE auth.uid()::text || '-%'
);
-- Policy: Allow authenticated users to delete their own avatars
CREATE POLICY "Users can delete their own avatars"
ON storage.objects
FOR DELETE
TO authenticated
USING (
bucket_id = 'avatars' AND
(storage.foldername(name))[1] = auth.uid()::text OR
name LIKE auth.uid()::text || '-%'
);
-- Policy: Allow public read access to avatars (so images can be displayed)
CREATE POLICY "Public can view avatars"
ON storage.objects
FOR SELECT
TO public
USING (bucket_id = 'avatars');
-- =====================================================
-- Alternative: Simpler policies if you want users to upload
-- files directly to the bucket root (no folders)
-- =====================================================
-- If the above policies don't work, try these simpler ones:
-- DROP POLICY IF EXISTS "Users can upload their own avatars" ON storage.objects;
-- DROP POLICY IF EXISTS "Users can update their own avatars" ON storage.objects;
-- DROP POLICY IF EXISTS "Users can delete their own avatars" ON storage.objects;
-- DROP POLICY IF EXISTS "Public can view avatars" ON storage.objects;
-- CREATE POLICY "Users can upload avatars"
-- ON storage.objects
-- FOR INSERT
-- TO authenticated
-- WITH CHECK (bucket_id = 'avatars' AND name LIKE auth.uid()::text || '-%');
-- CREATE POLICY "Users can update their avatars"
-- ON storage.objects
-- FOR UPDATE
-- TO authenticated
-- USING (bucket_id = 'avatars' AND name LIKE auth.uid()::text || '-%')
-- WITH CHECK (bucket_id = 'avatars' AND name LIKE auth.uid()::text || '-%');
-- CREATE POLICY "Users can delete their avatars"
-- ON storage.objects
-- FOR DELETE
-- TO authenticated
-- USING (bucket_id = 'avatars' AND name LIKE auth.uid()::text || '-%');
-- CREATE POLICY "Public can view avatars"
-- ON storage.objects
-- FOR SELECT
-- TO public
-- USING (bucket_id = 'avatars');
-- =====================================================
-- NOTES:
-- =====================================================
-- 1. Make sure the "avatars" bucket is created in Supabase Dashboard
-- - Go to Storage > Create bucket
-- - Name: "avatars"
-- - Public bucket: YES (so images can be accessed via URL)
--
-- 2. The policies above ensure:
-- - Users can only upload/update/delete files that start with their user ID
-- - Public can view all avatars (needed for displaying images)
--
-- 3. File naming convention: {user-id}-{timestamp}.{ext}
-- Example: 838e0b39-a805-410c-b6c8-c9526e255002-1234567890.jpg
--
-- 4. If you get permission errors, check:
-- - Bucket is set to "Public" in Storage settings
-- - Policies are correctly applied
-- - User is authenticated when uploading
-- =====================================================