Skip to content

Commit b3b4dfc

Browse files
committed
Captcha for download #3465
1 parent 783729d commit b3b4dfc

3 files changed

Lines changed: 24 additions & 50 deletions

File tree

web/core/Link/DownloadableLink.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { LinkType } from '@/types'
1717
import { ArrowRight } from '../../icons'
1818
import { BaseLink } from './BaseLink'
1919
import { getArrowElement, type ResourceLinkProps } from './ResourceLink'
20+
import { verifyCaptcha } from '@/lib/actions/verifyCaptcha'
2021

2122
type Variants = 'default' | 'fit'
2223
type Type = 'simple' | 'resource' | 'stickyMenu'
@@ -117,19 +118,13 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(
117118
async (event: any) => {
118119
const solution = event.detail.response
119120
setIsFriendlyChallengeDone(true)
120-
const response = await fetch('/api/download/getFileUrl', {
121-
body: JSON.stringify({
122-
frcCaptchaSolution: solution,
123-
}),
124-
headers: {
125-
'Content-Type': 'application/json',
126-
},
127-
method: 'POST',
128-
})
129-
console.log('response')
130-
if (!response.ok) {
131-
setNotHuman(true)
132-
}
121+
const result = await verifyCaptcha(solution)
122+
if(result !== true){
123+
setNotHuman(false)
124+
}
125+
else {
126+
setNotHuman(true)
127+
}
133128
},
134129
[],
135130
)

web/lib/actions/getFileUrl.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

web/lib/actions/verifyCaptcha.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use server"
2+
import { validateCaptcha } from '@/app/api/forms/validateCaptcha'
3+
4+
export async function verifyCaptcha(frcCaptchaSolution:any): Promise<boolean|{error:string}> {
5+
try {
6+
const { accept, errorCode } = await validateCaptcha(frcCaptchaSolution)
7+
if (!accept) {
8+
console.log(`Anti-robot check failed [code=${errorCode}] for file download`)
9+
return { error: `Anti-robot check failed [code=${errorCode}], please try again.` }
10+
}
11+
return true
12+
} catch (err) {
13+
console.error('Error occured while attempting to validate captcha', err)
14+
return { error: 'failed to validate captcha' }
15+
}
16+
}

0 commit comments

Comments
 (0)