Skip to content

docs: use user id when storing user's avatar image #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Examples/UserManagement/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,15 @@ struct ProfileView: View {
private func uploadImage() async throws -> String? {
guard let data = avatarImage?.data else { return nil }

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

try await supabase.storage
.from("avatars")
.upload(
path: filePath,
file: data,
options: FileOptions(contentType: "image/jpeg")
options: FileOptions(contentType: "image/jpeg", upsert: true)
)

return filePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@ insert into storage.buckets(id, name)

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

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

create policy "Anyone can update their own avatar." on storage.objects
for update
using (auth.uid() = owner)
with check (bucket_id = 'avatars');
create policy "Give users access to own folder 1oj01fe_2" on storage.objects
for update to authenticated
using (bucket_id = 'avatars'
and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));

create policy "Give users access to own folder 1oj01fe_3" on storage.objects
for delete to authenticated
using (bucket_id = 'avatars'
and LOWER(auth.uid()::text) = LOWER((storage.foldername(name))[1]));

16 changes: 10 additions & 6 deletions Sources/Storage/StorageFileApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public class StorageFileApi: StorageApi {
/// - file: The Data to be stored in the bucket.
/// - options: HTTP headers. For example `cacheControl`
@discardableResult
public func upload(path: String, file: Data, options: FileOptions = FileOptions())
async throws -> String
{
public func upload(
path: String,
file: Data,
options: FileOptions = FileOptions()
) async throws -> String {
try await uploadOrUpdate(method: .post, path: path, file: file, options: options)
}

Expand All @@ -84,9 +86,11 @@ public class StorageFileApi: StorageApi {
/// - file: The Data to be stored in the bucket.
/// - options: HTTP headers. For example `cacheControl`
@discardableResult
public func update(path: String, file: Data, options: FileOptions = FileOptions())
async throws -> String
{
public func update(
path: String,
file: Data,
options: FileOptions = FileOptions()
) async throws -> String {
try await uploadOrUpdate(method: .put, path: path, file: file, options: options)
}

Expand Down