Skip to content

Commit

Permalink
Add screen hint to getAuthorizationUrl (#1001)
Browse files Browse the repository at this point in the history
## Description

## Documentation

Does this require changes to the WorkOS Docs? E.g. the [API
Reference](https://workos.com/docs/reference) or code snippets need
updates.

```
[ ] Yes
```

If yes, link a related docs PR and add a docs maintainer as a reviewer.
Their approval is required.
  • Loading branch information
hadihallak authored Mar 29, 2024
1 parent 85bc34b commit 2fccd6c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ exports[`UserManagement getAuthorizationUrl with a custom api hostname generates

exports[`UserManagement getAuthorizationUrl with a provider generates an authorize url with the provider 1`] = `"https://api.workos.com/user_management/authorize?client_id=proj_123&provider=Google&redirect_uri=example.com%2Fauth%2Fworkos%2Fcallback&response_type=code"`;

exports[`UserManagement getAuthorizationUrl with a screenHint generates an authorize url with a screenHint 1`] = `"https://api.workos.com/user_management/authorize?client_id=proj_123&provider=authkit&redirect_uri=example.com%2Fauth%2Fworkos%2Fcallback&response_type=code&screen_hint=sign-up"`;

exports[`UserManagement getAuthorizationUrl with an organizationId generates an authorization URL with the organization 1`] = `"https://api.workos.com/user_management/authorize?client_id=proj_123&organization_id=organization_123&redirect_uri=example.com%2Fauth%2Fworkos%2Fcallback&response_type=code"`;

exports[`UserManagement getAuthorizationUrl with no custom api hostname generates an authorize url with the default api hostname 1`] = `"https://api.workos.com/user_management/authorize?client_id=proj_123&provider=Google&redirect_uri=example.com%2Fauth%2Fworkos%2Fcallback&response_type=code"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface AuthorizationURLOptions {
provider?: string;
redirectUri: string;
state?: string;
screenHint?: 'sign-up' | 'sign-in';
}
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 @@ -810,6 +810,21 @@ describe('UserManagement', () => {
});

describe('getAuthorizationUrl', () => {
describe('with a screenHint', () => {
it('generates an authorize url with a screenHint', () => {
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

const url = workos.userManagement.getAuthorizationUrl({
provider: 'authkit',
clientId: 'proj_123',
redirectUri: 'example.com/auth/workos/callback',
screenHint: 'sign-up',
});

expect(url).toMatchSnapshot();
});
});

describe('with no custom api hostname', () => {
it('generates an authorize url with the default api hostname', () => {
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
Expand Down
8 changes: 8 additions & 0 deletions src/user-management/user-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,20 @@ export class UserManagement {
provider,
redirectUri,
state,
screenHint,
}: AuthorizationURLOptions): string {
if (!provider && !connectionId && !organizationId) {
throw new TypeError(
`Incomplete arguments. Need to specify either a 'connectionId', 'organizationId', or 'provider'.`,
);
}

if (provider !== 'authkit' && screenHint) {
throw new TypeError(
`'screenHint' is only supported for 'authkit' provider`,
);
}

const query = toQueryString({
connection_id: connectionId,
organization_id: organizationId,
Expand All @@ -552,6 +559,7 @@ export class UserManagement {
redirect_uri: redirectUri,
response_type: 'code',
state,
screen_hint: screenHint,
});

return `${this.workos.baseURL}/user_management/authorize?${query}`;
Expand Down

0 comments on commit 2fccd6c

Please sign in to comment.