-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f43f9e2
commit 0a3ecf1
Showing
22 changed files
with
267 additions
and
168 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,9 @@ | ||
import { Fields, Files } from 'formidable'; | ||
import { IncomingMessage } from 'http'; | ||
|
||
declare module 'next' { | ||
export interface NextApiRequest extends IncomingMessage { | ||
fields?: Fields; | ||
files?: Files; | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './verifyCaptcha'; | ||
export * from './multipartyParse'; |
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 formidable from 'formidable'; | ||
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
/** | ||
* Parses multipart/form-data | ||
*/ | ||
export const multipartyParse = | ||
(handler: NextApiHandler, options: formidable.Options) => | ||
(req: NextApiRequest, res: NextApiResponse) => { | ||
const form = formidable(options); | ||
|
||
form.parse(req, async (err, fields, files) => { | ||
if (err) { | ||
console.error(err); | ||
res.status(400).json({ status: 'fail' }); | ||
return; | ||
} | ||
|
||
// Extend `req` object with parsed fields and files | ||
req.fields = fields; | ||
req.files = files; | ||
|
||
handler(req, res); | ||
}); | ||
}; |
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,43 @@ | ||
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { HCAPTCHA_VERIFY_URL } from '../constants'; | ||
|
||
const { HCAPTCHA_SECRET } = process.env; | ||
|
||
/** | ||
* Verifies client captcha token against hCaptcha endpoint | ||
*/ | ||
export const verifyCaptcha = | ||
(handler: NextApiHandler) => async (req: NextApiRequest, res: NextApiResponse) => { | ||
let captchaToken; | ||
|
||
if (req.fields?.captchaToken) { | ||
captchaToken = req.fields.captchaToken; | ||
} | ||
|
||
if (req.body?.captchaToken) { | ||
captchaToken = req.body.captchaToken; | ||
} | ||
|
||
if (!captchaToken) { | ||
return res.status(400).json({ status: 'captcha failed' }); | ||
} | ||
|
||
const response = await fetch(HCAPTCHA_VERIFY_URL, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
body: `response=${captchaToken}&secret=${HCAPTCHA_SECRET}` | ||
}); | ||
|
||
if (!response.ok) { | ||
return res.status(400).json({ status: 'captcha failed' }); | ||
} | ||
|
||
const { success } = await response.json(); | ||
|
||
if (!success) { | ||
return res.status(400).json({ status: 'captcha failed' }); | ||
} | ||
|
||
return handler(req, res); | ||
}; |
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
Oops, something went wrong.