0x0x230x

Untitled

Apr 26th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. const supabase = createClient();
  2. const user = await getProfile();
  3.  
  4. if (!user) {
  5. throw new Error(
  6. 'You must be authenticated to upload a profile picture'
  7. );
  8. }
  9.  
  10. const fileName = `${user.id}.jpg`;
  11. const filePath = fileName;
  12.  
  13. if (!user.avatar_url) {
  14. await supabase.storage.from('profile-pictures').upload(filePath, file);
  15. } else {
  16. // Replace (update) the file at the same path
  17.  
  18. const { data, error: updateError } = await supabase.storage
  19. .from('profile-pictures')
  20. .update(filePath, file, {
  21. upsert: true
  22. });
  23.  
  24. console.log('data', data);
  25.  
  26. if (updateError) {
  27. console.error('Update error:', updateError);
  28. throw updateError;
  29. }
  30. }
  31.  
  32. // Get the public URL
  33. const {
  34. data: { publicUrl }
  35. } = supabase.storage.from('profile-pictures').getPublicUrl(filePath);
  36.  
  37. // Update user profile with new avatar URL
  38. await updateProfile({
  39. avatarUrl: publicUrl
  40. });
  41.  
  42. return { publicUrl };
Advertisement
Add Comment
Please, Sign In to add comment