Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Allow authenticated users to read their own profile picture
- CREATE POLICY "Users can read their own profile picture"
- ON storage.objects
- FOR SELECT
- TO authenticated
- USING (bucket_id = 'profile-pictures' AND name = (SELECT auth.uid() || '.jpg'));
- -- Allow authenticated users to insert their own profile picture
- CREATE POLICY "Users can insert their own profile picture"
- ON storage.objects
- FOR INSERT
- TO authenticated
- WITH CHECK (bucket_id = 'profile-pictures' AND name = (SELECT auth.uid() || '.jpg'));
- -- Allow authenticated users to update their own profile picture
- CREATE POLICY "Users can update their own profile picture"
- ON storage.objects
- FOR UPDATE
- TO authenticated
- USING (bucket_id = 'profile-pictures' AND name = (SELECT auth.uid() || '.jpg'));
- ================================================+
- const supabase = createClient();
- const user = await getProfile();
- if (!user) {
- throw new Error(
- 'You must be authenticated to upload a profile picture'
- );
- }
- const fileName = `${user.id}.jpg`;
- const filePath = fileName;
- if (!user.avatar_url) {
- await supabase.storage.from('profile-pictures').upload(filePath, file);
- } else {
- // Replace (update) the file at the same path
- const { data, error: updateError } = await supabase.storage
- .from('profile-pictures')
- .update(filePath, file, {
- upsert: true
- });
- console.log('data', data);
- if (updateError) {
- console.error('Update error:', updateError);
- throw updateError;
- }
- }
- // Get the public URL
- const {
- data: { publicUrl }
- } = supabase.storage.from('profile-pictures').getPublicUrl(filePath);
- // Update user profile with new avatar URL
- await updateProfile({
- avatarUrl: publicUrl
- });
- return { publicUrl };
- }
Advertisement
Add Comment
Please, Sign In to add comment