Skip to content

Commit 5d53971

Browse files
authored
Use /users/sessions/token and password grant type (#790)
## Description `/users/sessions/token?grant_type=password` is the endpoint for password-based sessions going forward. ## 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.
1 parent d9a8550 commit 5d53971

6 files changed

+24
-15
lines changed
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
export interface AuthenticateUserWithPasswordOptions {
2+
clientId: string;
23
email: string;
34
password: string;
45
ipAddress?: string;
56
userAgent?: string;
6-
startSession?: boolean;
77
expiresIn?: number;
88
}
99

10+
export interface AuthenticateUserWithPasswordCredentials {
11+
clientSecret: string | undefined;
12+
}
13+
1014
export interface SerializedAuthenticateUserWithPasswordOptions {
15+
grant_type: 'password';
16+
client_id: string;
17+
client_secret: string | undefined;
1118
email: string;
1219
password: string;
1320
ip_address?: string;
1421
user_agent?: string;
15-
start_session?: boolean;
1622
expires_in?: number;
1723
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
export interface AuthenticateUserWithTokenOptions {
22
clientId: string;
33
code: string;
4-
startSession?: boolean;
54
expiresIn?: number;
65
}
76

87
export interface AuthenticateUserWithTokenCredentials {
98
clientSecret: string | undefined;
10-
grantType: string;
119
}
1210

1311
export interface SerializedAuthenticateUserWithTokenOptions {
12+
grant_type: 'authorization_code';
1413
client_id: string;
1514
client_secret: string | undefined;
1615
code: string;
17-
grant_type: string;
18-
start_session?: boolean;
1916
expires_in?: number;
2017
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import {
2+
AuthenticateUserWithPasswordCredentials,
23
AuthenticateUserWithPasswordOptions,
34
SerializedAuthenticateUserWithPasswordOptions,
45
} from '../interfaces';
56

67
export const serializeAuthenticateUserWithPasswordOptions = (
7-
options: AuthenticateUserWithPasswordOptions,
8+
options: AuthenticateUserWithPasswordOptions &
9+
AuthenticateUserWithPasswordCredentials,
810
): SerializedAuthenticateUserWithPasswordOptions => ({
11+
grant_type: 'password',
12+
client_id: options.clientId,
13+
client_secret: options.clientSecret,
914
email: options.email,
1015
password: options.password,
1116
ip_address: options.ipAddress,
1217
user_agent: options.userAgent,
13-
start_session: options.startSession,
1418
expires_in: options.expiresIn,
1519
});

src/users/serializers/authenticate-user-with-token-options.serializer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ export const serializeAuthenticateUserWithTokenOptions = (
88
options: AuthenticateUserWithTokenOptions &
99
AuthenticateUserWithTokenCredentials,
1010
): SerializedAuthenticateUserWithTokenOptions => ({
11+
grant_type: 'authorization_code',
1112
client_id: options.clientId,
1213
client_secret: options.clientSecret,
1314
code: options.code,
14-
grant_type: options.grantType,
15-
start_session: options.startSession,
1615
expires_in: options.expiresIn,
1716
});

src/users/users.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,17 @@ describe('UserManagement', () => {
9898

9999
describe('authenticateUserWithPassword', () => {
100100
it('sends an password authentication request', async () => {
101-
mock.onPost('/users/authentications').reply(200, {
101+
mock.onPost('/users/sessions/token').reply(200, {
102102
user: userFixture,
103103
session: sessionFixture,
104104
});
105105
const resp = await workos.users.authenticateUserWithPassword({
106+
clientId: 'proj_whatever',
106107
107108
password: 'extra-secure',
108109
});
109110

110-
expect(mock.history.post[0].url).toEqual('/users/authentications');
111+
expect(mock.history.post[0].url).toEqual('/users/sessions/token');
111112
expect(resp).toMatchObject({
112113
user: {
113114

src/users/users.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ export class Users {
8383
any,
8484
SerializedAuthenticateUserWithPasswordOptions
8585
>(
86-
'/users/authentications',
87-
serializeAuthenticateUserWithPasswordOptions(payload),
86+
'/users/sessions/token',
87+
serializeAuthenticateUserWithPasswordOptions({
88+
...payload,
89+
clientSecret: this.workos.key,
90+
}),
8891
);
8992

9093
return deserializeAuthenticationResponse(data);
@@ -102,7 +105,6 @@ export class Users {
102105
serializeAuthenticateUserWithTokenOptions({
103106
...payload,
104107
clientSecret: this.workos.key,
105-
grantType: 'authorization_code',
106108
}),
107109
);
108110

0 commit comments

Comments
 (0)