Skip to content

cannot authenticate user  #645

@shawnesquivel

Description

@shawnesquivel

Describe the bug
Even after verifying the user is authenticated, I cannot access my data.

I've confirmed through print statements that

  • My user is authenticated as it generates me the access token
  • The SQL is valid (works when I turn off RLS or if I provide the access_token to the cURL request)

I created two simple RLS policies for allowing

  • inserts for authenticated users
  • selects for authenticated users

CREATE POLICY "Enable insert for authenticated users only" ON "public"."todos"
AS PERMISSIVE FOR INSERT
TO authenticated

WITH CHECK (true)

To Reproduce
Steps to reproduce the behavior:

  1. Create a table called todos with these columns
image
  1. Create a RLS for insert
CREATE POLICY "Enable insert for authenticated users only" ON "public"."todos"
AS PERMISSIVE FOR INSERT
TO authenticated

WITH CHECK (true)

  1. Use this code snippet as mentioned in RLS not working after sign-in #185
    user = supabase.auth.sign_in_with_password({"email": email, "password": password})
    session = supabase.auth.get_session()  # this refreshes the access token if needed
    # tried this first
    supabase.postgrest.auth(
        # verified that this was real
        token=session.access_token
    )  # this sets the right "authorization" header on HTTP requests to PostgREST
    res = supabase.auth.set_session(session.access_token, session.refresh_token)
    select_data = supabase.table("todos").select("*").execute()
    print(select_data)
    insert_data = supabase.table("todos").insert({"name": "Buy milk"}).execute()
    print(insert_data)

Results:
Data is empty

data=[] count=None

RLS error for insert:

    insert_data = supabase.table("todos").insert({"name": "Buy milk"}).execute()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/postgrest/_sync/request_builder.py", line 70, in execute
    raise APIError(r.json())
postgrest.exceptions.APIError: {'code': '42501', 'details': None, 'hint': None, 'message': 'new row violates row-level security policy for table "todos"'}

Expected behavior
As tested with RLS off, I can retrieve the contents of the rows.
I can also view do a cURL request with the access_token of the session and retrieve the rows.
So my best guess is that although the user itself is successfully authenticated, there's no way for us to successfully tell the client which executes the queries, that we have given it an authenticated user, even though I have used the workarounds mentioned by the community.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Mac 14.1.2
  • Version: 2.3.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions