Skip to content

Commit e4a362f

Browse files
committed
Captcha validation
1 parent 1eee22e commit e4a362f

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

packages/next-drupal/src/components/utils/Widgets/CaptchaWidget.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import cx from 'classnames'
44

55
import WidgetError from '../../theme/Errors/WidgetError'
66

7-
const FRIENDLYCAPTCHA_SITEKEY: string = process?.env
8-
?.NEXT_PUBLIC_FRIENDLYCAPTCHA_SITEKEY
9-
? process.env.NEXT_PUBLIC_FRIENDLYCAPTCHA_SITEKEY
10-
: ''
7+
const FRIENDLYCAPTCHA_SITEKEY: string =
8+
process.env.NEXT_PUBLIC_FRIENDLYCAPTCHA_SITEKEY || ''
119

1210
export function CaptchaWidget({ id, InputProps = {}, error }: any) {
1311
const captchaRef: any = useRef(null)
@@ -28,7 +26,7 @@ export function CaptchaWidget({ id, InputProps = {}, error }: any) {
2826
onChange.current(value)
2927
},
3028
errorCallback: () => {
31-
onChange.current(true)
29+
onChange.current()
3230
},
3331
sitekey: FRIENDLYCAPTCHA_SITEKEY,
3432
})

packages/next-drupal/src/config/Validations.tsx

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,60 @@ const defaultTextValidation = ({ name, required }) => {
1212
return validation
1313
}
1414

15+
const emailValidation = ({ name, required }) => {
16+
let validation = yup.string().email('Must be a valid email')
17+
if (required) {
18+
validation = validation.required(`${capitalize(name)} is required`)
19+
}
20+
return validation
21+
}
22+
23+
const friendlycaptchaValidation = () => {
24+
return yup
25+
.string()
26+
.test('captcha', 'Captcha verification is invalid', async (solution) => {
27+
if (process.env.NODE_ENV === 'development') {
28+
return true
29+
}
30+
const body = new FormData()
31+
body.append(
32+
'secret',
33+
process.env.NEXT_PUBLIC_FRIENDLYCAPTCHA_SECRET || '',
34+
)
35+
body.append(
36+
'sitekey',
37+
process.env.NEXT_PUBLIC_FRIENDLYCAPTCHA_SITEKEY || '',
38+
)
39+
body.append('solution', solution || '')
40+
41+
try {
42+
const response = await fetch(
43+
'https://api.friendlycaptcha.com/api/v1/siteverify',
44+
{
45+
body,
46+
headers: {
47+
'Content-Type': 'application/json',
48+
},
49+
method: 'POST',
50+
},
51+
)
52+
const result: any = await response.text()
53+
if (result?.success) {
54+
return true
55+
}
56+
return false
57+
} catch (e) {
58+
return false
59+
}
60+
})
61+
.required('Captcha verification is required')
62+
}
63+
1564
// Validations mapping
1665
export const validationsMapping = {
1766
type: {
18-
captcha: () => {
19-
return yup.string().required('Captcha verification is required')
20-
},
21-
email: ({ name, required }) => {
22-
let validation = yup.string().email('Must be a valid email')
23-
if (required) {
24-
validation = validation.required(`${capitalize(name)} is required`)
25-
}
26-
return validation
27-
},
67+
email: emailValidation,
68+
headless_captcha: friendlycaptchaValidation,
2869
text: defaultTextValidation,
2970
textarea: defaultTextValidation,
3071
textfield: defaultTextValidation,

packages/next-drupal/src/config/Widgets.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const defaultWidget = TextWidget
1010
// Widgets mapping
1111
export const widgetsMapping = {
1212
type: {
13-
captcha: CaptchaWidget,
1413
email: EmailWidget,
14+
headless_captcha: CaptchaWidget,
1515
text: defaultWidget,
1616
textarea: TextareaWidget,
1717
textfield: defaultWidget,

turbo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"DRUPAL_CLIENT_ID",
2020
"DRUPAL_CLIENT_SECRET",
2121
"NEXT_PUBLIC_DRAFT_REPORT_TERM_IDS",
22+
"NEXT_PUBLIC_FRIENDLYCAPTCHA_SECRET",
2223
"NEXT_PUBLIC_FRIENDLYCAPTCHA_SITEKEY"
2324
],
2425
"pipeline": {

0 commit comments

Comments
 (0)