imtiaz
02/02/2022, 10:43 PMjs
try {
      if (!event.target.files || event.target.files.length == 0) {
        throw 'You must select an image to upload.'
      }
      const user = supabase.auth.user()
      const file = event.target.files[0]
      const fileExt = file.name.split('.').pop()
      const fileName = `a1jg6w_0/${user.id}.${fileExt}`
      const filePath = `${fileName}`
      let { error: uploadError } = await supabase.storage
        .from('profile-pictures')
        .upload(filePath, file)
      console.log('uploading')
      if (uploadError) {
        throw uploadError
      }
      console.log('uploaded')
      let { error: updateError } = await supabase.from('profiles').upsert({
        id: user.id,
        avatar_url: filePath,
      })
      if (updateError) {
        throw updateError
      }
      setProfilePicture(filePath)
    } catch (error) {
      alert(error.message)
    }