Skip to content

Commit

Permalink
feat: add sign_out() scope option (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Smith committed Dec 7, 2023
2 parents 1440dd6 + 34a3ddf commit 4ec8842
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
4 changes: 3 additions & 1 deletion 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,14 @@ 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",
query={"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"])

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)

def on_auth_state_change(
self,
Expand Down
4 changes: 3 additions & 1 deletion 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,14 @@ 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",
query={"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"])

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

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 4ec8842

Please sign in to comment.