Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return_to parameter to get_logout_url #400

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading