Skip to content

Commit

Permalink
feat: add sign_out() scope option
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks committed Dec 7, 2023
1 parent 1440dd6 commit 3818dba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
5 changes: 3 additions & 2 deletions gotrue/_async/gotrue_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
GenerateLinkParams,
GenerateLinkResponse,
Options,
SignOutScope,
User,
UserResponse,
)
Expand All @@ -39,13 +40,13 @@ def __init__(
self.mfa.list_factors = self._list_factors
self.mfa.delete_factor = self._delete_factor

async def sign_out(self, jwt: str) -> None:
async def sign_out(self, jwt: str, scope: SignOutScope = "global") -> None:
"""
Removes a logged-in session.
"""
return await self._request(
"POST",
"logout",
f"logout?scope={scope}",
jwt=jwt,
no_resolve_json=True,
)
Expand Down
10 changes: 6 additions & 4 deletions gotrue/_async/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
SignInWithOAuthCredentials,
SignInWithPasswordCredentials,
SignInWithPasswordlessCredentials,
SignOutOptions,
SignUpWithPasswordCredentials,
Subscription,
UserAttributes,
Expand Down Expand Up @@ -480,7 +481,7 @@ async def refresh_session(
session = await self._call_refresh_token(refresh_token)
return AuthResponse(session=session, user=session.user)

async def sign_out(self) -> None:
async def sign_out(self, options: SignOutOptions = {"scope": "global"}) -> None:
"""
Inside a browser context, `sign_out` will remove the logged in user from the
browser session and log them out - removing all items from localstorage and
Expand All @@ -496,10 +497,11 @@ async def sign_out(self) -> None:
session = await self.get_session()
access_token = session.access_token if session else None
if access_token:
await self.admin.sign_out(access_token)
await self.admin.sign_out(access_token, options["scope"])

Check warning on line 500 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L500

Added line #L500 was not covered by tests

await self._remove_session()
self._notify_all_subscribers("SIGNED_OUT", None)
if options["scope"] != "others":
await self._remove_session()
self._notify_all_subscribers("SIGNED_OUT", None)

Check warning on line 504 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L502-L504

Added lines #L502 - L504 were not covered by tests

def on_auth_state_change(
self,
Expand Down
5 changes: 3 additions & 2 deletions gotrue/_sync/gotrue_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
GenerateLinkParams,
GenerateLinkResponse,
Options,
SignOutScope,
User,
UserResponse,
)
Expand All @@ -39,13 +40,13 @@ def __init__(
self.mfa.list_factors = self._list_factors
self.mfa.delete_factor = self._delete_factor

def sign_out(self, jwt: str) -> None:
def sign_out(self, jwt: str, scope: SignOutScope = "global") -> None:
"""
Removes a logged-in session.
"""
return self._request(
"POST",
"logout",
f"logout?scope={scope}",
jwt=jwt,
no_resolve_json=True,
)
Expand Down
10 changes: 6 additions & 4 deletions gotrue/_sync/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
SignInWithOAuthCredentials,
SignInWithPasswordCredentials,
SignInWithPasswordlessCredentials,
SignOutOptions,
SignUpWithPasswordCredentials,
Subscription,
UserAttributes,
Expand Down Expand Up @@ -478,7 +479,7 @@ def refresh_session(self, refresh_token: Union[str, None] = None) -> AuthRespons
session = self._call_refresh_token(refresh_token)
return AuthResponse(session=session, user=session.user)

def sign_out(self) -> None:
def sign_out(self, options: SignOutOptions = {"scope": "global"}) -> None:
"""
Inside a browser context, `sign_out` will remove the logged in user from the
browser session and log them out - removing all items from localstorage and
Expand All @@ -494,10 +495,11 @@ def sign_out(self) -> None:
session = self.get_session()
access_token = session.access_token if session else None
if access_token:
self.admin.sign_out(access_token)
self.admin.sign_out(access_token, options["scope"])

Check warning on line 498 in gotrue/_sync/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_sync/gotrue_client.py#L498

Added line #L498 was not covered by tests

self._remove_session()
self._notify_all_subscribers("SIGNED_OUT", None)
if options["scope"] != "others":
self._remove_session()
self._notify_all_subscribers("SIGNED_OUT", None)

Check warning on line 502 in gotrue/_sync/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_sync/gotrue_client.py#L500-L502

Added lines #L500 - L502 were not covered by tests

def on_auth_state_change(
self,
Expand Down
7 changes: 7 additions & 0 deletions gotrue/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,13 @@ class DecodedJWTDict(TypedDict):
amr: NotRequired[Union[List[AMREntry], None]]


SignOutScope = Literal["global", "local", "others"]


class SignOutOptions(TypedDict):
scope: NotRequired[SignOutScope]


for model in [
AMREntry,
AuthResponse,
Expand Down

0 comments on commit 3818dba

Please sign in to comment.