-
Notifications
You must be signed in to change notification settings - Fork 1
/
captcha.ts
49 lines (47 loc) · 1.49 KB
/
captcha.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
type ErrorCodes =
| 'missing-input-secret'
| 'invalid-input-secret'
| 'missing-input-response'
| 'invalid-input-response'
| 'bad-request'
| 'invalid-or-already-seen-response'
| 'sitekey-secret-mismatch'
export type Response = {
/* eslint-disable camelcase -- 3rd-party api naming */
success: boolean
challenge_ts: string
hostname: string
credit?: boolean
'error-codes'?: ErrorCodes[]
/* eslint-enable camelcase -- 3rd-party api naming */
}
export const validateToken = (token: string): Promise<boolean> => {
return new Promise((resolve, reject) => {
if (process.env.NODE_ENV !== 'production') {
resolve(true)
return
}
fetch('https://hcaptcha.com/siteverify', {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
body: `response=${token}&secret=${process.env.CAPTCHA_SECRET}`,
})
.then((response) => {
if (response.status < 200 || response.status >= 300)
throw new Error('There was an error validating the token.')
return response.json()
})
.then((data: Response) => {
if (data.success) {
resolve(true)
return
}
throw data['error-codes']
})
.catch((error) => {
reject(error)
})
})
}