From 84c9dd0ad25b8a20ac6e5c8bd6c467fd97a607c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vinko=20=C5=A0mid?= Date: Thu, 15 Feb 2024 08:54:41 +0100 Subject: [PATCH] captcha cleanup and fix. --- backend/src/lib/captcha.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/backend/src/lib/captcha.ts b/backend/src/lib/captcha.ts index 905f970..0add447 100644 --- a/backend/src/lib/captcha.ts +++ b/backend/src/lib/captcha.ts @@ -10,6 +10,7 @@ export type Captcha = { eKey: string; token: string }; * @returns {Promise} */ export async function checkCaptcha(captchaToken: string): Promise { + //Skip check for local_dev and test environment if ( [AppEnvironment.LOCAL_DEV, AppEnvironment.TEST].includes( env.APP_ENV as AppEnvironment @@ -18,20 +19,16 @@ export async function checkCaptcha(captchaToken: string): Promise { return true; } + //If captcha is not configured, skip check if (!env.CAPTCHA_SECRET) { - throwCodeException(ValidatorErrorCode.CAPTCHA_NOT_CONFIGURED); + return true; } if (!captchaToken) { throwCodeException(ValidatorErrorCode.CAPTCHA_NOT_PRESENT); } - if ( - env.APP_ENV != AppEnvironment.LOCAL_DEV! && - (await verifyCaptcha(captchaToken)) - ) { - throwCodeException(ValidatorErrorCode.CAPTCHA_INVALID); - } + await verifyCaptcha(captchaToken); return true; } @@ -44,7 +41,7 @@ async function verifyCaptcha( return (await verify(secret, token)).success; } catch (err) { console.error("Error verifying captcha!", err); - throw err; + throwCodeException(ValidatorErrorCode.CAPTCHA_INVALID); } }