-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate new event form with captcha
- Loading branch information
Showing
5 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import querystring from 'querystring' | ||
|
||
import { RECAPTCHA_SERVER_KEY } from 'src/app.config' | ||
|
||
import { logger } from './logger' | ||
|
||
const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify' | ||
|
||
/** Return true if the given captcha token was valid, false otherwise. */ | ||
export async function validateCaptcha(token: string): Promise<boolean> { | ||
try { | ||
const data = { secret: RECAPTCHA_SERVER_KEY, response: token } | ||
const res = await fetch(RECAPTCHA_ENDPOINT, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
body: querystring.stringify(data), | ||
}) | ||
const json = await res.json() | ||
logger.debug({ json }, 'reCAPTCHA response') | ||
return json.success | ||
} catch (err) { | ||
logger.error({ err }, 'Failed to validate reCAPTCHA') | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters