Skip to content

Commit 5e99a5e

Browse files
committed
feat(auth): add sign_out method to reset session and trigger auth event
1 parent c81ffba commit 5e99a5e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

supabase/_sync/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ def _listen_to_auth_events(
300300

301301
self.options.headers["Authorization"] = self._create_auth_header(access_token)
302302

303+
def sign_out(self) -> None:
304+
"""Signs out the current user by revoking the session token."""
305+
self.auth.sign_out()
306+
self._listen_to_auth_events("SIGNED_OUT", None)
307+
303308

304309
def create_client(
305310
supabase_url: str,

tests/test_client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,47 @@ def test_updates_the_authorization_header_on_auth_events() -> None:
9191

9292
assert client.storage.session.headers.get("apiKey") == key
9393
assert client.storage.session.headers.get("Authorization") == updated_authorization
94+
95+
96+
def test_sign_out_resets_auth_headers() -> None:
97+
url = os.environ.get("SUPABASE_TEST_URL")
98+
key = os.environ.get("SUPABASE_TEST_KEY")
99+
100+
client = create_client(url, key)
101+
102+
mock_session = MagicMock(access_token="secretuserjwt")
103+
client._listen_to_auth_events("SIGNED_IN", mock_session)
104+
105+
updated_authorization = f"Bearer {mock_session.access_token}"
106+
assert client.options.headers.get("Authorization") == updated_authorization
107+
108+
client.sign_out()
109+
110+
assert client.options.headers.get("Authorization") == f"Bearer {key}"
111+
assert client._postgrest is None
112+
assert client._storage is None
113+
assert client._functions is None
114+
115+
116+
def test_sign_out_calls_auth_sign_out() -> None:
117+
url = os.environ.get("SUPABASE_TEST_URL")
118+
key = os.environ.get("SUPABASE_TEST_KEY")
119+
120+
client = create_client(url, key)
121+
client.auth.sign_out = MagicMock()
122+
123+
client.sign_out()
124+
125+
client.auth.sign_out.assert_called_once()
126+
127+
128+
def test_sign_out_triggers_auth_event() -> None:
129+
url = os.environ.get("SUPABASE_TEST_URL")
130+
key = os.environ.get("SUPABASE_TEST_KEY")
131+
132+
client = create_client(url, key)
133+
client._listen_to_auth_events = MagicMock()
134+
135+
client.sign_out()
136+
137+
client._listen_to_auth_events.assert_called_once_with("SIGNED_OUT", None)

0 commit comments

Comments
 (0)