express-recaptcha icon indicating copy to clipboard operation
express-recaptcha copied to clipboard

Error: callback is required

Open SrS2225a opened this issue 2 years ago • 4 comments

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)

SrS2225a avatar Sep 23 '22 06:09 SrS2225a

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

chandowd avatar May 24 '23 17:05 chandowd

+1 :/

NoPurposeInLife avatar Jan 18 '24 02:01 NoPurposeInLife

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.

SrS2225a avatar Jan 22 '24 22:01 SrS2225a

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) {...

chriscant avatar Jun 25 '24 18:06 chriscant