Skip to content

Commit

Permalink
Add return_to parameter to get_logout_url
Browse files Browse the repository at this point in the history
  • Loading branch information
mthadley committed Jan 13, 2025
1 parent cab11e9 commit df3746d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tests/test_user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ def test_get_logout_url(self):

assert expected == result

def test_get_logout_url_with_return_to(self):
expected = "https://api.workos.test/user_management/sessions/logout?session_id=session_123&return_to=https%3A%2F%2Fexample.com%2Fsigned-out"
result = self.user_management.get_logout_url(
"session_123", return_to="https://example.com/signed-out"
)

assert expected == result


@pytest.mark.sync_and_async(UserManagement, AsyncUserManagement)
class TestUserManagement(UserManagementFixtures):
Expand Down
11 changes: 9 additions & 2 deletions workos/user_management.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional, Protocol, Sequence, Type, cast
from urllib.parse import urlencode
from workos._client_configuration import ClientConfiguration
from workos.session import Session
from workos.types.list_resource import (
Expand Down Expand Up @@ -588,19 +589,25 @@ def get_jwks_url(self) -> str:

return f"{self._client_configuration.base_url}sso/jwks/{self._client_configuration.client_id}"

def get_logout_url(self, session_id: str) -> str:
def get_logout_url(self, session_id: str, return_to: Optional[str] = None) -> str:
"""Get the URL for ending the session and redirecting the user
This method is purposefully designed as synchronous as it does not make any HTTP requests.
Args:
session_id (str): The ID of the user's session
return_to (str): The URL to redirect the user to after the session is ended. (Optional)
Returns:
(str): URL to redirect the user to to end the session.
"""

return f"{self._client_configuration.base_url}user_management/sessions/logout?session_id={session_id}"
params = {"session_id": session_id}

if return_to:
params["return_to"] = return_to

return f"{self._client_configuration.base_url}user_management/sessions/logout?{urlencode(params)}"

def get_password_reset(self, password_reset_id: str) -> SyncOrAsync[PasswordReset]:
"""Get the details of a password reset object.
Expand Down

0 comments on commit df3746d

Please sign in to comment.