supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
2.86k stars 220 forks source link

Table INSERT subscriptions fail with Error 401: Not Unauthorized #848

Open vyknight opened 8 months ago

vyknight commented 8 months ago

Describe the bug

I've subscribed to inserts on a table using the supabase api, when I add a row into that same table on the supabase web interface I receive notice that a change has occurred but not the content of the row that was inserted. I've ensured that the table has been added to the supabase-realtime channel just like the guide. I have tried to disable/enable RLS, giving all permissions to both anon and public roles, and also tried to use the api service key. None of these resolved the 401: unauthorized error.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

Code to subscribe to channel, which I copied from the API tab on the web interface.

const { createClient } = require('@supabase/supabase-js')

const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY)

const coachMessages = supabase.channel('custom-insert-channel')
  .on(
    'postgres_changes',
    { event: 'INSERT', schema: 'public', table: 'coach_messages' },
    (payload: any) => {
      console.log('Change received!', payload)
    }
  )
  .subscribe()

console.log(coachMessages)

and the error:

image

Row insertion and database actions are done using the supabase web interface.

Expected behavior

A clear and concise description of what you expected to happen.

The payload to contain the row that was just inserted in the payload.new property.

System information

iamgmd commented 8 months ago

I am experiencing the same problem in 2.33.2. Is there a fallback version I can use until this bug is fixed?

btw.. I am using @nuxtjs/supabase and therefore const supabase = useSupabaseClient(); to create the subscription. I assume that it is a wrapper around @supabase/supabase-js.

mfissehaye commented 8 months ago

I am having the same problem.

iamgmd commented 8 months ago

Ok, I think I got this to work but I am not sure as to why, maybe someone who is advanced in this topic maybe able to shed some light.

I turned off RLS for the table followed by:

grant select on table to anon;

as documented in Bring your own database

after I did that, I get the payload.

UPDATE: I needed both anon and authenticated for my shopping cart so:

grant select on transactions to anon, authenticated;

mfissehaye commented 8 months ago

Ok, I think I got this to work but I am not sure as to why, maybe someone who is advanced in this topic maybe able to shed some light.

I turned off RLS for the table followed by:

grant select on table to anon;

as documented in Bring your own database

after I did that, I get the payload.

UPDATE: I needed both anon and authenticated for my shopping cart so:

grant select on transactions to anon, authenticated;

That makes it work. Thank you.

vyknight commented 8 months ago

Ok, I think I got this to work but I am not sure as to why, maybe someone who is advanced in this topic maybe able to shed some light.

I turned off RLS for the table followed by:

grant select on table to anon;

as documented in Bring your own database

after I did that, I get the payload.

UPDATE: I needed both anon and authenticated for my shopping cart so:

grant select on transactions to anon, authenticated;

This worked for me as well, thank a lot.

johnsutor commented 7 months ago

Make sure you have the Authorization header set on the webhook as well, as that can cause a 401

peterj commented 5 months ago

The "Missing grants" section in the troubleshooting docs helper me fix this issue: https://supabase.com/partners/integrations/prisma#troubleshooting

yellow-mi commented 2 weeks ago

I get an error when I try to set anon for my table, any other workaround?