Refresh user token simply with Phone Number
Hello!
Im using supabase-py on the backend.
My flow is quite simple: user creates an account in our React native front end, OTP confirmation is already setup and then they are redirected to send a message on whatsapp, as most of the interactions happen there (via twilio).
My goal is to perform db operations using simply the user's phone number.
So our main endpoint receives whatsapp messages. Once the user signsup, everytime the user sends a whatsapp message to the endpoint, we should refresh that token and i want to store every interaction in supabase with their phone number.
So I created a few functions:
I've been chatting with your docs for hours and hours, to no success.
I tried two approaches: using the supabase python client and using the api directly.
When using the python client, even though I'm initiating the create client using:
client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
Its not creating an instance of Supabaseclient, but rather syncclient (see screenshot) and I keep getting errors like:
So I went the purely API route. Still going in circles, even after attempting different endpoints, function versions and so on. What am I doing wrong?
and yes, I am testing with a phone number that is registered in the database
Any clues on how to solve this?
Im using supabase-py on the backend.
My flow is quite simple: user creates an account in our React native front end, OTP confirmation is already setup and then they are redirected to send a message on whatsapp, as most of the interactions happen there (via twilio).
My goal is to perform db operations using simply the user's phone number.
So our main endpoint receives whatsapp messages. Once the user signsup, everytime the user sends a whatsapp message to the endpoint, we should refresh that token and i want to store every interaction in supabase with their phone number.
So I created a few functions:
I've been chatting with your docs for hours and hours, to no success.
I tried two approaches: using the supabase python client and using the api directly.
When using the python client, even though I'm initiating the create client using:
client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
Its not creating an instance of Supabaseclient, but rather syncclient (see screenshot) and I keep getting errors like:
... langchain/backend/services/auth_service.py", line 76, in refresh_user_token_and_get_user_id
supabase.from_table("auth.users")
^^^^^^^^^^^^^^^^^^^
AttributeError: 'SyncClient' object has no attribute 'from_table'So I went the purely API route. Still going in circles, even after attempting different endpoints, function versions and so on. What am I doing wrong?
and yes, I am testing with a phone number that is registered in the database
Code
Auth service
async def read_auth_record(phone_number: str):
client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
url_path = f"/rest/v1/auth/users?phone=eq.{phone_number}"
headers = {"Authorization": f"Bearer {SUPABASE_SERVICE_KEY}"}
async with httpx.AsyncClient() as http_client:
response = await http_client.get(SUPABASE_URL + url_path, headers=headers)
if response.status_code == 200 and len(response.json()) > 0:
return response.json()[0]
return Noneasync def get_user_id_and_refresh_token(phone_number: str):
# Get user from auth.users table using phone number
user_response = await read_auth_record(phone_number)
if user_response is None:
raise Exception(f"User with phone number {phone_number} not found")
user_id = user_response["id"]
# Get refresh token from auth.refresh_tokens table using user id
refresh_token_query = {"columns": "token", "eq": {"user_id": user_id}}
refresh_token_response = await read_auth_record(refresh_token_query)
if refresh_token_response is None:
raise Exception(f"Refresh token not found for user with id {user_id}")
refresh_token = refresh_token_response["token"]
return user_id, refresh_tokenasync def refresh_user_access_token(phone_number: str):
try:
user_id, refresh_token = await get_user_id_and_refresh_token(phone_number)
if not refresh_token:
raise Exception("Refresh token not found")
access_token = await refresh_access_token(refresh_token)
return access_token
except Exception as e:
logger.error(f"Exception occurred: {e}")
raiseError
INFO:services.onboarding:***PROCESS WA MESSAGE*** Received WhatsApp message:whatsapp:+5511989422063 B
INFO:httpx:HTTP Request: GET https://hrezykteprxueblmoehb.supabase.co/rest/v1/auth/users?phone=eq.+5511989422063 "HTTP/1.1 401 Unauthorized"
ERROR:services.onboarding:***PROCESS WA MESSAGE*** Unhandled exception: User with phone number +5511989422063 not found
ERROR:services.onboarding:Traceback (most recent call last):
File "/Users/olivergattermayr/Documents/DEV/la-langchain/backend/services/onboarding.py", line 170, in process_whatsapp_message
user_id, refresh_token = await get_user_id_and_refresh_token(user_number)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/olivergattermayr/Documents/DEV/la-langchain/backend/services/auth_service.py", line 40, in get_user_id_and_refresh_tok
Any clues on how to solve this?