Skip to content

Commit 90ec9ce

Browse files
authored
docs: use user id when storing user's avatar image (#228)
1 parent 042acc0 commit 90ec9ce

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

Examples/UserManagement/ProfileView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ struct ProfileView: View {
167167
private func uploadImage() async throws -> String? {
168168
guard let data = avatarImage?.data else { return nil }
169169

170-
let filePath = "\(UUID().uuidString).jpeg"
170+
let userId = try await supabase.auth.session.user.id.uuidString
171+
let filePath = "\(userId)/profile.jpeg"
171172

172173
try await supabase.storage
173174
.from("avatars")
174175
.upload(
175176
path: filePath,
176177
file: data,
177-
options: FileOptions(contentType: "image/jpeg")
178+
options: FileOptions(contentType: "image/jpeg", upsert: true)
178179
)
179180

180181
return filePath

Examples/UserManagement/supabase/migrations/20240125111924_init.sql

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,23 @@ insert into storage.buckets(id, name)
4949

5050
-- Set up access controls for storage.
5151
-- See https://supabase.com/docs/guides/storage/security/access-control#policy-examples for more details.
52-
create policy "Avatar images are publicly accessible." on storage.objects
53-
for select
54-
using (bucket_id = 'avatars');
52+
create policy "Give users access to own folder 1oj01fe_0" on storage.objects
53+
for select to authenticated
54+
using (bucket_id = 'avatars'
55+
and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));
5556

56-
create policy "Anyone can upload an avatar." on storage.objects
57-
for insert
58-
with check (bucket_id = 'avatars');
57+
create policy "Give users access to own folder 1oj01fe_1" on storage.objects
58+
for insert to authenticated
59+
with check (bucket_id = 'avatars'
60+
and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));
5961

60-
create policy "Anyone can update their own avatar." on storage.objects
61-
for update
62-
using (auth.uid() = owner)
63-
with check (bucket_id = 'avatars');
62+
create policy "Give users access to own folder 1oj01fe_2" on storage.objects
63+
for update to authenticated
64+
using (bucket_id = 'avatars'
65+
and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));
66+
67+
create policy "Give users access to own folder 1oj01fe_3" on storage.objects
68+
for delete to authenticated
69+
using (bucket_id = 'avatars'
70+
and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));
6471

Sources/Storage/StorageFileApi.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public class StorageFileApi: StorageApi {
7171
/// - file: The Data to be stored in the bucket.
7272
/// - options: HTTP headers. For example `cacheControl`
7373
@discardableResult
74-
public func upload(path: String, file: Data, options: FileOptions = FileOptions())
75-
async throws -> String
76-
{
74+
public func upload(
75+
path: String,
76+
file: Data,
77+
options: FileOptions = FileOptions()
78+
) async throws -> String {
7779
try await uploadOrUpdate(method: .post, path: path, file: file, options: options)
7880
}
7981

@@ -84,9 +86,11 @@ public class StorageFileApi: StorageApi {
8486
/// - file: The Data to be stored in the bucket.
8587
/// - options: HTTP headers. For example `cacheControl`
8688
@discardableResult
87-
public func update(path: String, file: Data, options: FileOptions = FileOptions())
88-
async throws -> String
89-
{
89+
public func update(
90+
path: String,
91+
file: Data,
92+
options: FileOptions = FileOptions()
93+
) async throws -> String {
9094
try await uploadOrUpdate(method: .put, path: path, file: file, options: options)
9195
}
9296

0 commit comments

Comments
 (0)