fix: refresh token updates auth headers and sets realtime auth#1171
fix: refresh token updates auth headers and sets realtime auth#1171Kabya002 wants to merge 3 commits intosupabase:mainfrom
Conversation
|
Hi @Kabya002 thanks for your first contribution. Can you provide some unit tests around the issue that this is fixing please? |
e22cf0a to
dfd22c0
Compare
|
-Added tests for both sync and async client token refresh behavior |
|
Thanks for the note, @silentworks! |
|
I’m currently working on refactoring the fix to ensure that auth.admin.* routes always use the service_role key and aren't affected by user token refresh logic. I appreciate you pointing this out, and I’ll update the PR soon to reflect this improved approach. If there are any references, patterns, or internal logic you'd recommend looking into (especially around how the client enforces separation of admin vs. user scopes), I’d really appreciate it! Thanks again for the guidance — I’ll post an update shortly once I’ve implemented the fix. |
Summary
This PR fixes a critical bug where the Supabase client fails to update the Authorization header after the access token is refreshed, which caused privileged requests like
supabase.auth.admin.list_users()to fail with a 403 error.The bug was tracked in #1143.
What Was the Bug?
on_auth_state_change), theAuthorizationheader inclient.options.headerswas not being updated.Authorizationheader, failed.What This Fix Does
_listen_to_auth_events()on bothSyncClientandAsyncClient.auth.on_auth_state_change(...)in the client constructor.SIGNED_IN,TOKEN_REFRESHED, orSIGNED_OUT:Authorizationheader with the new access token.realtime.set_auth(...).Tests Added
I added new unit tests in both sync and async test suites that verify:
Authorizationheader updates correctly after a token refresh.postgrest,storage,functions) are set toNoneso they reinitialize with the correct headers.set_auth(...).Test Files:
tests/_sync/test_auth_refresh_sync.pytests/_async/test_auth_refresh_async.pyAll tests pass.
Closes