Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

supabase auth.uid() = user_id equals false

I have a very strange problem that is driving we up the walls. I am creating the following table

CREATE TABLE articles(
  id BIGINT generated by default as IDENTITY PRIMARY KEY,
  user_id uuid references auth.users NOT NULL,
  title TEXT,
  CONTENT TEXT,
  user_email TEXT,
  inserted_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::TEXT,NOW()) NOT NULL
);

ALTER TABLE articles ENABLE ROW LEVEL SECURITY;

CREATE policy "users can create articles" ON articles for
INSERT WITH CHECK(auth.uid() = user_id);

CREATE policy "users can update their own articles" ON articles for
UPDATE USING (auth.uid() = user_id);

CREATE policy "users can delete their own articles" ON articles for
DELETE USING (auth.uid() = user_id);

CREATE policy "users can read their own articles" ON articles for
SELECT USING (auth.uid() = user_id);

I can add data to it through my front-end with:

            const { data, error } = await supabaseClient.from("articles").insert(
                [{
                    title: title,
                    content: content,
                    user_email: user?.email?.toLowerCase(),
                    user_id: user?.id,
                }]
            ).single()

and that works. I can verify on supabase that the row has indeed been added.

Entry got added

However, I can't access my data through the front-end since the expression: auth.uid() = user_id seems to evaluate to false even though it is the same. If I disable Row level security or write TRUE instead everything works as intended.

enter image description here

like image 756
frankBang Avatar asked Apr 23 '26 09:04

frankBang


1 Answers

Can you use Typecast, it seems to work for me earlier.

CHECK(auth.uid()::text = user_id::text);
like image 79
Anmoldeep Singh Avatar answered Apr 25 '26 01:04

Anmoldeep Singh