Skip to content

Commit

Permalink
captcha cleanup and fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkoS993 committed Feb 15, 2024
1 parent f7e973c commit 84c9dd0
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions backend/src/lib/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Captcha = { eKey: string; token: string };
* @returns {Promise<boolean>}
*/
export async function checkCaptcha(captchaToken: string): Promise<boolean> {
//Skip check for local_dev and test environment
if (
[AppEnvironment.LOCAL_DEV, AppEnvironment.TEST].includes(
env.APP_ENV as AppEnvironment
Expand All @@ -18,20 +19,16 @@ export async function checkCaptcha(captchaToken: string): Promise<boolean> {
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;
}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 84c9dd0

Please sign in to comment.