Skip to content

Commit e62a5e6

Browse files
Fix listAuthFactors() return (#862)
## 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.
1 parent 0e88cda commit e62a5e6

File tree

5 files changed

+83
-24
lines changed

5 files changed

+83
-24
lines changed

src/users/fixtures/factor.json

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"object": "list",
3+
"data": [
4+
{
5+
"object": "authentication_factor",
6+
"id": "auth_factor_1234",
7+
"created_at": "2022-03-15T20:39:19.892Z",
8+
"updated_at": "2022-03-15T20:39:19.892Z",
9+
"type": "totp",
10+
"totp": {
11+
"issuer": "WorkOS",
12+
"qr_code": "qr-code-test",
13+
"secret": "secret-test",
14+
"uri": "uri-test",
15+
"user": "some_user"
16+
}
17+
}
18+
],
19+
"list_metadata": {
20+
"before": null,
21+
"after": null
22+
}
23+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export interface ListAuthFactorsOptions {
1+
import { PaginationOptions } from '../../common/interfaces';
2+
3+
export interface ListAuthFactorsOptions extends PaginationOptions {
24
userId: string;
35
}

src/users/users.spec.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import MockAdapter from 'axios-mock-adapter';
33
import { WorkOS } from '../workos';
44
import userFixture from './fixtures/user.json';
55
import listUsersFixture from './fixtures/list-users.json';
6-
import factorFixture from './fixtures/factor.json';
6+
import listFactorFixture from './fixtures/list-factors.json';
77

88
const mock = new MockAdapter(axios);
99
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
@@ -410,7 +410,20 @@ describe('UserManagement', () => {
410410
describe('enrollAuthFactor', () => {
411411
it('sends an enrollAuthFactor request', async () => {
412412
mock.onPost(`/users/${userId}/auth/factors`).reply(200, {
413-
authentication_factor: factorFixture,
413+
authentication_factor: {
414+
object: 'authentication_factor',
415+
id: 'auth_factor_1234',
416+
created_at: '2022-03-15T20:39:19.892Z',
417+
updated_at: '2022-03-15T20:39:19.892Z',
418+
type: 'totp',
419+
totp: {
420+
issuer: 'WorkOS',
421+
qr_code: 'qr-code-test',
422+
secret: 'secret-test',
423+
uri: 'uri-test',
424+
user: 'some_user',
425+
},
426+
},
414427
authentication_challenge: {
415428
object: 'authentication_challenge',
416429
id: 'auth_challenge_1234',
@@ -460,13 +473,35 @@ describe('UserManagement', () => {
460473

461474
describe('listAuthFactors', () => {
462475
it('sends a listAuthFactors request', async () => {
463-
mock.onGet(`/users/${userId}/auth/factors`).reply(200, [factorFixture]);
476+
mock.onGet(`/users/${userId}/auth/factors`).reply(200, listFactorFixture);
464477

465478
const resp = await workos.users.listAuthFactors({ userId });
466479

467480
expect(mock.history.get[0].url).toEqual(`/users/${userId}/auth/factors`);
468481

469-
expect(resp[0]).toMatchObject({ id: factorFixture.id });
482+
expect(resp).toMatchObject({
483+
object: 'list',
484+
data: [
485+
{
486+
object: 'authentication_factor',
487+
id: 'auth_factor_1234',
488+
createdAt: '2022-03-15T20:39:19.892Z',
489+
updatedAt: '2022-03-15T20:39:19.892Z',
490+
type: 'totp',
491+
totp: {
492+
issuer: 'WorkOS',
493+
qrCode: 'qr-code-test',
494+
secret: 'secret-test',
495+
uri: 'uri-test',
496+
user: 'some_user',
497+
},
498+
},
499+
],
500+
listMetadata: {
501+
before: null,
502+
after: null,
503+
},
504+
});
470505
});
471506
});
472507

src/users/users.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,25 @@ export class Users {
298298
};
299299
}
300300

301-
async listAuthFactors(payload: ListAuthFactorsOptions): Promise<Factor[]> {
302-
const { data } = await this.workos.get<FactorResponse[]>(
303-
`/users/${payload.userId}/auth/factors`,
301+
async listAuthFactors(
302+
options: ListAuthFactorsOptions,
303+
): Promise<AutoPaginatable<Factor>> {
304+
return new AutoPaginatable(
305+
await fetchAndDeserialize<FactorResponse, Factor>(
306+
this.workos,
307+
`/users/${options.userId}/auth/factors`,
308+
deserializeFactor,
309+
options,
310+
),
311+
(params) =>
312+
fetchAndDeserialize<FactorResponse, Factor>(
313+
this.workos,
314+
`/users/${options.userId}/auth/factors`,
315+
deserializeFactor,
316+
params,
317+
),
318+
options,
304319
);
305-
306-
return data.map(deserializeFactor);
307320
}
308321

309322
async deleteUser(payload: DeleteUserOptions) {

0 commit comments

Comments
 (0)