Skip to content

Commit

Permalink
fix: replace hcaptcha by recaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGOs92 committed Dec 21, 2023
1 parent d303875 commit 4802101
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ KEYCLOAK_CLIENT_ID=
KEYCLOAK_CLIENT_SECRET=
NEXTAUTH_URL=
NEXTAUTH_SECRET=
RE_CAPTCHA_SECRET_KEY=
HCAPTCHA_SECRET_KEY=
2 changes: 1 addition & 1 deletion server/lib/mailer/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const mailSchema = {
email: { valid: validEmail, isRequired: true, type: "string" },
message: { isRequired: true, type: "string" },
subject: { isRequired: true, type: "string" },
reCaptchaToken: { isRequired: true, type: "string" },
captchaToken: { isRequired: true, type: "string" },
};

module.exports = {
Expand Down
16 changes: 10 additions & 6 deletions server/lib/mailer/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,29 @@ async function sendTemplateMail(templateKey) {
return true
}

async function checkReCaptcha(reCaptchaToken) {
const response = await fetch(`https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RE_CAPTCHA_SECRET_KEY}&response=${reCaptchaToken}`, {
method: 'POST'
async function checkCaptcha(captchaToken) {
const response = await fetch(`https://api.hcaptcha.com/siteverify`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `response=${captchaToken}&secret=${process.env.HCAPTCHA_SECRET_KEY}`,
})

const json = await response.json()

if (!json.success) {
throw new Error('Le reCaptcha est invalide')
throw new Error('Le captcha est invalide')
}

return json.success
}

async function sendFormContactMail(payload) {
const validatedPayload = validPayload(payload, mailSchema)
const {reCaptchaToken, ...emailData } = validatedPayload
const {captchaToken, ...emailData } = validatedPayload

await checkReCaptcha(reCaptchaToken)
await checkCaptcha(captchaToken)

const contactTemplate = templates.contact(emailData)

Expand Down

0 comments on commit 4802101

Please sign in to comment.