Skip to content

Commit

Permalink
validateEmail() returns boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
mw10013 committed Jan 5, 2024
1 parent 7e1ad22 commit a83360c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface SendTOTP {
* @param email The email address to validate.
*/
export interface ValidateEmail {
(email: string): Promise<void>
(email: string): Promise<boolean>
}

/**
Expand Down Expand Up @@ -414,7 +414,9 @@ export class TOTPStrategy<User> extends Strategy<User, TOTPVerifyParams> {
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(),
Expand Down Expand Up @@ -469,7 +471,7 @@ export class TOTPStrategy<User> extends Strategy<User, TOTPVerifyParams> {

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({
Expand Down
7 changes: 4 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit a83360c

Please sign in to comment.