hotbelgo
02/06/2022, 5:43 PMosaxma
02/06/2022, 5:50 PMauthenticated
using:
sql
-- this sets the role for the transaction only
SET role authenticated;
-- try to insert the player manually
insert into players (user_id, name) values ('id', 'some_name');
I believe this will give you a better message and easier to debug than from the client sideosaxma
02/06/2022, 6:00 PMauth.role()
is called instead of role()
aloneosaxma
02/06/2022, 6:00 PMhotbelgo
02/06/2022, 6:08 PMALTER POLICY "Special " ON public.players USING ((auth.role() = 'authenticated'::text)) WITH CHECK ((auth.role() = 'authenticated'::text));
but it always comes back subsequently with the auth removedosaxma
02/06/2022, 6:11 PMgaryaustin
02/06/2022, 6:13 PMgaryaustin
02/06/2022, 6:17 PMgaryaustin
02/06/2022, 6:18 PMhotbelgo
02/06/2022, 6:22 PMosaxma
02/06/2022, 6:23 PMsql
-- to avoid bypassing the RLS
SET role authenticated;
-- for the policy to receive the correct value (maybe you need to set it to null afterwards)
select set_config('request.jwt.claims.role', 'authenticated');
garyaustin
02/06/2022, 6:23 PMhotbelgo
02/06/2022, 6:25 PMfunction set_config(unknown, unknown) does not exist
garyaustin
02/06/2022, 6:26 PMhotbelgo
02/06/2022, 6:27 PMgaryaustin
02/06/2022, 6:28 PMosaxma
02/06/2022, 6:29 PMsql
select set_config('request.jwt.claims.role', 'authenticated', true); -- true means for the transaction only, false for the session
garyaustin
02/06/2022, 6:29 PMgaryaustin
02/06/2022, 6:31 PMosaxma
02/06/2022, 6:48 PMsql
SELECT set_config('request.jwt.claims', jsonb_build_object('role', 'authenticated')::jsonb::text, true)::text;
osaxma
02/06/2022, 6:55 PMsql
create table test (
id text,
value text
);
alter table public.test enable ROW LEVEL SECURITY;
CREATE POLICY "Enable insert for authenticated users only"
ON public.test
FOR INSERT WITH CHECK (
auth.role() = 'authenticated'
);
CREATE POLICY "Enable select for authenticated user"
ON public.test
FOR SELECT USING (
auth.role() = 'authenticated'
);
When I set role()
instead of auth.role()
-- I get an error function role() does not exist
(in the local studio, it should show auth.role()
in the UI -- at least recent version using the cli).
Running the following:
sql
set role authenticated;
insert into test values ('1', '1');
gives the following error:
sql
new row violates row-level security policy for table "test"
On the other hand, running the following:
sql
set role authenticated;
SELECT set_config('request.jwt.claims', jsonb_build_object('role', 'authenticated')::jsonb::text, true)::text;
insert into test values ('1', '1');
gives the following results:
Success. No rows returned
I'm sharing this just as a way to debug ...
If you're using the cli for local development, maybe try upgrading to the newest version.garyaustin
02/06/2022, 7:24 PMhotbelgo
02/06/2022, 7:28 PMhotbelgo
02/06/2022, 7:29 PMSET role authenticated;
SELECT set_config('request.jwt.claims', jsonb_build_object('role', 'authenticated')::jsonb::text, true)::text;
insert into players (user_id, name) values ('....', 'some_name');
got a successgaryaustin
02/06/2022, 7:32 PMhotbelgo
02/06/2022, 7:35 PMfetch("https://xyz.supabase.co/rest/v1/players?columns=%22user_id%22%2C%22name%22", {
"headers": {
"accept": "*/*",
"accept-language": "en-GB,en;q=0.9,en-US;q=0.8,nl;q=0.7,fr;q=0.6,es;q=0.5",
"apikey": "abc123",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9......",
"cache-control": "no-cache",
"content-profile": "public",
"content-type": "application/json",
"pragma": "no-cache",
"prefer": "return=representation",
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"97\", \"Chromium\";v=\"97\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"x-client-info": "supabase-js/1.29.1"
},
"referrer": "http://localhost:3000/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": "[{\"user_id\":\"abc123\",\"name\":\"HB\"}]",
"method": "POST",
"mode": "cors",
"credentials": "include"
});
hotbelgo
02/06/2022, 7:36 PMgaryaustin
02/06/2022, 7:39 PMhotbelgo
02/06/2022, 7:40 PMgaryaustin
02/06/2022, 7:41 PMgaryaustin
02/06/2022, 7:41 PMgaryaustin
02/06/2022, 7:42 PMhotbelgo
02/06/2022, 7:42 PMgaryaustin
02/06/2022, 7:43 PMhotbelgo
02/06/2022, 7:44 PMhotbelgo
02/06/2022, 7:46 PMdata
hotbelgo
02/06/2022, 7:46 PMhotbelgo
02/06/2022, 7:46 PMgaryaustin
02/06/2022, 7:51 PMgaryaustin
02/06/2022, 7:51 PMhotbelgo
02/06/2022, 9:03 PM