File: //var/www/quadcode.com/src/routes/api/CheckingRecaptcha.ts
import { env } from '$env/dynamic/private';
import WLogger from './WLogger';
import Sanitizing from './Sanitizing';
const CheckingRecaptcha = (token: string) => {
WLogger.log('info', '[Recaptcha]: START', Sanitizing({ token: token }));
if (!env?.PRIVATE_RECAPTCHA_SECRET) {
WLogger.log('error', '[Recaptcha]: END', {
error: `PRIVATE_RECAPTCHA_SECRET: undefined`,
});
return;
}
const data = {
secret: env.PRIVATE_RECAPTCHA_SECRET,
response: token,
};
return fetch(`https://www.google.com/recaptcha/api/siteverify`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams(Object.entries(data)).toString(),
})
.then((response) => response.json())
.then((response) => {
return {
...response,
error: ENRecaptchaError[response['error-codes']?.[0]],
};
})
.catch((error) => error);
};
const ENRecaptchaError: { [name: string]: string } = {
'missing-input-secret': 'The secret parameter is missing.',
'invalid-input-secret': 'The secret parameter is invalid or malformed.',
'missing-input-response': 'The response parameter is missing.',
'invalid-input-response': 'The response parameter is invalid or malformed.',
'bad-request': 'The request is invalid or malformed.',
'timeout-or-duplicate ': 'The response is no longer valid: either is too old or has been used previously.',
};
export default CheckingRecaptcha;