pdupavillon / express-recaptcha

Implementation of google recaptcha v2 & V3 solutions for express.js
MIT License
128 stars 22 forks source link

Error: callback is required #63

Open SrS2225a opened 1 year ago

SrS2225a commented 1 year ago

When attempting to render the recaptcha, I would get an vague error only saying that "callback is required", but as far as I can tell I am doing just that. I copied the code in the example and did not change much else. What am I missing?

Here is the full error in question:

Error: callback is required
    at RecaptchaV3.renderWith (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express-recaptcha/dist/v3.js:65:19)
    at RecaptchaV3.render (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express-recaptcha/dist/v3.js:54:21)
    at render (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express-recaptcha/dist/v3.js:32:43)
    at Layer.handle [as handle_request] (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/layer.js:95:5)
    at /home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/index.js:335:12)
    at next (/home/sawyer/Documents/code/WebstormProjects/nyx website/node_modules/express/lib/router/index.js:275:10)
chandowd commented 1 year ago

Did you ever fix this error? I'm also getting it after following the steps outlined in the example.

NoPurposeInLife commented 5 months ago

+1 :/

SrS2225a commented 5 months ago

Hello, not sure how I missed this, but I eventually decided to do it manually instead of using this middleware, as I was not able to fix the error. It was still pretty easy to add recaptcha without the help of this middleware, so I can share the details of how I accomplished this without it if interested.

chriscant commented 1 week ago

FYI: in practice options always needs a callback parameter as per examples lower down on README.md and the supplied example. Without using the middleware, this is what I did:

const Recaptcha = require('express-recaptcha').RecaptchaV3
const options = { callback: 'cb' }
const recaptcha = new Recaptcha(process.env.RECAPTCHA_SITE_KEY, process.env.RECAPTCHA_SECRET_KEY, options)

The cb function is in client-side JavaScript, eg:

let gRecaptchaResponse = null
function cb (token) {
  gRecaptchaResponse = token
}

You might then pass gRecaptchaResponse in an API call as the request body field 'g-recaptcha-response'.

Server-side you can then do this call which checks 'g-recaptcha-response' in req:

recaptcha.verify(req, function (error, data) {...