0x0x230x

Untitled

Apr 26th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. -- Allow authenticated users to read their own profile picture
  2. CREATE POLICY "Users can read their own profile picture"
  3. ON storage.objects
  4. FOR SELECT
  5. TO authenticated
  6. USING (bucket_id = 'profile-pictures' AND name = (SELECT auth.uid() || '.jpg'));
  7.  
  8. -- Allow authenticated users to insert their own profile picture
  9. CREATE POLICY "Users can insert their own profile picture"
  10. ON storage.objects
  11. FOR INSERT
  12. TO authenticated
  13. WITH CHECK (bucket_id = 'profile-pictures' AND name = (SELECT auth.uid() || '.jpg'));
  14.  
  15. -- Allow authenticated users to update their own profile picture
  16. CREATE POLICY "Users can update their own profile picture"
  17. ON storage.objects
  18. FOR UPDATE
  19. TO authenticated
  20. USING (bucket_id = 'profile-pictures' AND name = (SELECT auth.uid() || '.jpg'));
  21.  
  22.  
  23.  
  24.  
  25.  
  26. ================================================+
  27.  
  28.  
  29.  
  30. const supabase = createClient();
  31. const user = await getProfile();
  32.  
  33. if (!user) {
  34. throw new Error(
  35. 'You must be authenticated to upload a profile picture'
  36. );
  37. }
  38.  
  39. const fileName = `${user.id}.jpg`;
  40. const filePath = fileName;
  41.  
  42. if (!user.avatar_url) {
  43. await supabase.storage.from('profile-pictures').upload(filePath, file);
  44. } else {
  45. // Replace (update) the file at the same path
  46.  
  47. const { data, error: updateError } = await supabase.storage
  48. .from('profile-pictures')
  49. .update(filePath, file, {
  50. upsert: true
  51. });
  52.  
  53. console.log('data', data);
  54.  
  55. if (updateError) {
  56. console.error('Update error:', updateError);
  57. throw updateError;
  58. }
  59. }
  60.  
  61. // Get the public URL
  62. const {
  63. data: { publicUrl }
  64. } = supabase.storage.from('profile-pictures').getPublicUrl(filePath);
  65.  
  66. // Update user profile with new avatar URL
  67. await updateProfile({
  68. avatarUrl: publicUrl
  69. });
  70.  
  71. return { publicUrl };
  72. }
Advertisement
Add Comment
Please, Sign In to add comment