SupabaseS
Supabase6mo ago
Joece

RLS violation when attempting to upload to storage on astro api endpoint

Relevant Packages:
  • "@supabase/supabase-js": "^2.50.2",
  • "astro": "^5.10.1",
  • "@astrojs/react": "^4.3.0",
  • "react": "^19.1.0"
Hi all! I'm having an issue when I am trying to upload some files to supabase via an astro endpoint that's being called with
fetch
in my react component

I'm using anonymous sign in and i am passing the access token in the as an auth bearer as follows. The form data in this case is an array of images
const res = await fetch('/api/photos', {
                method: 'POST',
                headers: {
                    Authorization: `Bearer ${accessToken}`,
                },
                body: formData,
            })


in my api/photos endpoint, i am attempting to do the following
const {
        data: { user },
        error,
    } = await supabase.auth.getUser(accessToken)

// some additional checks here....

for (let file of files) {
        if (file instanceof File) {
            // Create a unique file path, e.g., using user ID and timestamp
            const filePath = `${user.id}/${Date.now()}-${file.name}`
            const { error } = await supabase.storage.from('photos').upload(filePath, file, {
                cacheControl: '3600',
                upsert: false,
            })

            console.log(error)
            if (error)
                return new Response(
                    JSON.stringify({
                        error: 'Something went wrong with picture upload',
                    }),
                    { status: 500 }
                )
        } else {
            return new Response(
                JSON.stringify({ error: `file upload failed for user-${user.id}/${Date.now()}` }),
                { status: 500 }
            )
        }
    }

Checking the user, the user is authenticated as expected but i am getting a status code "403" "new row violates row-level security policy" error. I am not sure what is happeing. Any help would be great!
Was this page helpful?