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 returnTo option to userManagement.getLogoutUrl #1200

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions src/user-management/user-management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,21 @@ describe('UserManagement', () => {
'https://api.workos.com/user_management/sessions/logout?session_id=123456',
);
});

describe('when a `returnTo` is given', () => {
it('includes a `return_to` in the URL', () => {
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

const url = workos.userManagement.getLogoutUrl({
sessionId: '123456',
returnTo: 'https://your-app.com/signed-out',
});

expect(url).toBe(
'https://api.workos.com/user_management/sessions/logout?session_id=123456&returnTo=https%3A%2F%2Fyour-app.com%2Fsigned-out',
);
});
});
});

describe('getLogoutUrlFromSessionCookie', () => {
Expand Down
20 changes: 18 additions & 2 deletions src/user-management/user-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,28 @@ export class UserManagement {
return `${this.workos.baseURL}/user_management/authorize?${query}`;
}

getLogoutUrl({ sessionId }: { sessionId: string }): string {
getLogoutUrl({
sessionId,
returnTo,
}: {
sessionId: string;
returnTo?: string;
}): string {
if (!sessionId) {
throw new TypeError(`Incomplete arguments. Need to specify 'sessionId'.`);
}

return `${this.workos.baseURL}/user_management/sessions/logout?session_id=${sessionId}`;
const url = new URL(
'/user_management/sessions/logout',
this.workos.baseURL,
);

url.searchParams.set('session_id', sessionId);
if (returnTo) {
url.searchParams.set('returnTo', returnTo);
}

return url.toString();
}

/**
Expand Down
Loading