Skip to content

Commit 6004d34

Browse files
authored
Merge pull request #70 from js-accounts/password-client
Add password recovery functions to client
2 parents a9aba6a + 8d7718c commit 6004d34

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

packages/client/src/AccountsClient.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,38 @@ export class AccountsClient {
241241
throw new AccountsError(err.message);
242242
}
243243
}
244+
245+
async verifyEmail(token: string): Promise<void> {
246+
try {
247+
await this.transport.verifyEmail(token);
248+
} catch (err) {
249+
throw new AccountsError(err.message);
250+
}
251+
}
252+
253+
async resetPassword(token: string, newPassword: string): Promise<void> {
254+
try {
255+
await this.transport.resetPassword(token, newPassword);
256+
} catch (err) {
257+
throw new AccountsError(err.message);
258+
}
259+
}
260+
261+
async requestPasswordReset(email?: string): Promise<void> {
262+
try {
263+
await this.transport.sendResetPasswordEmail(email);
264+
} catch (err) {
265+
throw new AccountsError(err.message);
266+
}
267+
}
268+
269+
async requestVerificationEmail(email?: string): Promise<void> {
270+
try {
271+
await this.transport.sendVerificationEmail(email);
272+
} catch (err) {
273+
throw new AccountsError(err.message);
274+
}
275+
}
244276
}
245277

246278
const Accounts = {
@@ -284,6 +316,18 @@ const Accounts = {
284316
refreshSession(): Promise<void> {
285317
return this.instance.refreshSession();
286318
},
319+
verifyEmail(token: string): Promise<void> {
320+
return this.instance.verifyEmail(token);
321+
},
322+
resetPassword(token: string, newPassword: string): Promise<void> {
323+
return this.instance.resetPassword(token, newPassword);
324+
},
325+
requestPasswordReset(email?: string): Promise<void> {
326+
return this.instance.requestPasswordReset(email);
327+
},
328+
requestVerificationEmail(email?: string): Promise<void> {
329+
return this.instance.requestVerificationEmail(email);
330+
},
287331
};
288332

289333
export default Accounts;

packages/client/src/AccountsClient.spec.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,89 @@ describe('Accounts', () => {
406406
// });
407407
// });
408408
});
409+
410+
describe('verifyEmail', () => {
411+
it('should return an AccountsError', async () => {
412+
const error = 'something bad';
413+
Accounts.config({}, { verifyEmail: () => Promise.reject({ message: error }) });
414+
try {
415+
await Accounts.verifyEmail();
416+
throw new Error();
417+
} catch (err) {
418+
expect(err.message).toEqual(error);
419+
}
420+
});
421+
422+
it('should call transport.verifyEmail', async () => {
423+
const mock = jest.fn(() => Promise.resolve());
424+
Accounts.config({}, { verifyEmail: mock });
425+
await Accounts.verifyEmail('token');
426+
expect(mock.mock.calls.length).toEqual(1);
427+
expect(mock.mock.calls[0][0]).toEqual('token');
428+
});
429+
});
430+
431+
describe('resetPassword', () => {
432+
it('should return an AccountsError', async () => {
433+
const error = 'something bad';
434+
Accounts.config({}, { resetPassword: () => Promise.reject({ message: error }) });
435+
try {
436+
await Accounts.resetPassword();
437+
throw new Error();
438+
} catch (err) {
439+
expect(err.message).toEqual(error);
440+
}
441+
});
442+
443+
it('should call transport.resetPassword', async () => {
444+
const mock = jest.fn(() => Promise.resolve());
445+
Accounts.config({}, { resetPassword: mock });
446+
await Accounts.resetPassword('token', 'newPassword');
447+
expect(mock.mock.calls.length).toEqual(1);
448+
expect(mock.mock.calls[0][0]).toEqual('token');
449+
expect(mock.mock.calls[0][1]).toEqual('newPassword');
450+
});
451+
});
452+
453+
describe('requestPasswordReset', () => {
454+
it('should return an AccountsError', async () => {
455+
const error = 'something bad';
456+
Accounts.config({}, { sendResetPasswordEmail: () => Promise.reject({ message: error }) });
457+
try {
458+
await Accounts.requestPasswordReset();
459+
throw new Error();
460+
} catch (err) {
461+
expect(err.message).toEqual(error);
462+
}
463+
});
464+
465+
it('should call transport.sendResetPasswordEmail', async () => {
466+
const mock = jest.fn(() => Promise.resolve());
467+
Accounts.config({}, { sendResetPasswordEmail: mock });
468+
await Accounts.requestPasswordReset('email');
469+
expect(mock.mock.calls.length).toEqual(1);
470+
expect(mock.mock.calls[0][0]).toEqual('email');
471+
});
472+
});
473+
474+
describe('requestVerificationEmail', () => {
475+
it('should return an AccountsError', async () => {
476+
const error = 'something bad';
477+
Accounts.config({}, { sendVerificationEmail: () => Promise.reject({ message: error }) });
478+
try {
479+
await Accounts.requestVerificationEmail();
480+
throw new Error();
481+
} catch (err) {
482+
expect(err.message).toEqual(error);
483+
}
484+
});
485+
486+
it('should call transport.sendVerificationEmail', async () => {
487+
const mock = jest.fn(() => Promise.resolve());
488+
Accounts.config({}, { sendVerificationEmail: mock });
489+
await Accounts.requestVerificationEmail('email');
490+
expect(mock.mock.calls.length).toEqual(1);
491+
expect(mock.mock.calls[0][0]).toEqual('email');
492+
});
493+
});
409494
});

0 commit comments

Comments
 (0)