From a83360c6074440321b5551dd9521bd17ca540d3b Mon Sep 17 00:00:00 2001 From: Michael Wu Date: Thu, 4 Jan 2024 20:47:05 -0500 Subject: [PATCH] validateEmail() returns boolean --- src/index.ts | 8 +++++--- test/index.spec.ts | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index fcaeeb9..e33ceb6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -123,7 +123,7 @@ export interface SendTOTP { * @param email The email address to validate. */ export interface ValidateEmail { - (email: string): Promise + (email: string): Promise } /** @@ -414,7 +414,9 @@ export class TOTPStrategy extends Strategy { formData: FormData options: RequiredAuthenticateOptions }) { - await this.validateEmail(email) + if (!(await this.validateEmail(email))) { + throw new Error(this.customErrors.invalidEmail) + } const { otp: code, ...totpPayload } = generateTOTP({ ...this.totpGeneration, secret: generateSecret(), @@ -469,7 +471,7 @@ export class TOTPStrategy extends Strategy { private async _validateEmailDefault(email: string) { const regexEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/gm - if (!regexEmail.test(email)) throw new Error(this.customErrors.invalidEmail) + return regexEmail.test(email) } private async _validateTOTP({ diff --git a/test/index.spec.ts b/test/index.spec.ts index 85f54c3..31b1da6 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -341,14 +341,15 @@ describe('[ TOTP ]', () => { }) }) - test('Should failure redirect when custom validateEmail throws Error.', async () => { + test('Should failure redirect when custom validateEmail returns false.', async () => { const ERROR_MESSAGE = 'TEST: Invalid email.' const strategy = new TOTPStrategy( { ...TOTP_STRATEGY_OPTIONS, - validateEmail: () => { - throw new Error(ERROR_MESSAGE) + customErrors: { + invalidEmail: ERROR_MESSAGE, }, + validateEmail: async () => false, }, verify, )