简体   繁体   English

ValidationError:不允许“ g-recaptcha-response”

[英]ValidationError: “g-recaptcha-response” is not allowed

I want to validate my registration page and i am using Joi validation every thing working fine except google recaptcha. 我想验证我的注册页面,并且我正在使用Joi验证,除了Google Recaptcha之外,其他所有功能都正常运行。

Here is code for form validation snippet, 这是表单验证代码段的代码,

var UserSchema = Joi.object().keys({
    name: Joi.string().required(),
    username: Joi.string().alphanum().min(4).max(30).required(),
    email: Joi.string().email().required(),
    mobile: Joi.string().required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{8,30}$/),
    confirmationPassword: Joi.any().valid(Joi.ref('password')).required(),
    access_token: [Joi.string(), Joi.number()],
    recaptcha : Joi.string()    
});

Post request for registration page, 发布注册请求页面,

router.route('/register')
    .get(isNotAuthenticated, (req, res) => {
        res.render('register');
    })
    .post(async (req, res, next) => {
        try {
            const data = Joi.validate(req.body, UserSchema);
            if (data.error) {
                req.flash("error", "Enter Valid data");
                console.log(data.error);
                res.redirect('/register');
                return;
            }
            const user = await User.findOne({ username: data.value.username });
            if (user) {
                req.flash('error', 'Username already taken');
                res.redirect('/register');
                return;
            }
            if (req.body.captcha === undefined ||
                req.body.captcha === '' ||
                req.body.captcha === null) {
                res.redirect('/register');
                return;
            }
            const secretKey = '';
            const verifyUrl = `https://google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${req.body.captcha}&remoteip=${req.connection.remoteAddress}`;
            request(verifyUrl, async (err, response, body) => {
                try{
                    body = JSON.parse(body);
                    console.log(body);
                    if (body.success !== undefined && !body.success) {
                        res.redirect('/register');
                        return;
                    } else {
                        const newUser = await new User(data.value);
                        console.log('newUser', newUser);
                        await newUser.save();
                    }
                }catch(e){
                    throw e;
                }

            });  
            req.flash('success', 'Please check your email.');
            res.redirect('/login');
        } catch (error) {
            next(error);
        }
    });

When i click on submit i got error message in console that { ValidationError: "g-recaptcha-response" is not allowed 当我单击提交时,我在控制台中收到错误消息,提示{ ValidationError: "g-recaptcha-response" is not allowed

You could access g-recaptcha-response using bracket notation. 您可以使用方括号表示法访问g-recaptcha-response。 So in your Joi schema the property should look something like this: 因此,在您的Joi模式中,属性应如下所示:

["g-recaptcha-response"]: Joi.string().required();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 #g-recaptcha-response 不要在 puppeteer 中加载 - #g-recaptcha-response dont load in puppeteer reCAPTCHA:缺少“g-recaptcha-response”的 POST 响应 - reCAPTCHA: Missing POST response for 'g-recaptcha-response' 不可见的reCAPTCHA发送具有多种形式的空g-captcha-chasponse - Invisible reCAPTCHA sending empty g-recaptcha-response with multiple forms 如何以在线形式删除 g-recaptcha-response? - How can I remove g-recaptcha-response in online form? 用户确认自己不是机器人后,如何获取recaptcha客户端验证参数g-recaptcha-response? - how to get recaptcha client verification parameter g-recaptcha-response after user confirms he is not a robot? 禁用提交按钮,直到 recaptcha3 g-recaptcha-response 值被检索 - Disable submit button until recaptcha3 g-recaptcha-response value is retrived $ _POST [“ g-recaptcha-response”],它在Captcha div元素中存储在哪里? 如何使用AJAX处理Recaptcha? - $_POST[“g-recaptcha-response”], where is it stored in the Captcha div element? How to handle Recaptcha with AJAX? 通过将g-recaptcha-response传递给javascript文件中的php文件来验证Google Recaptcha - Validating google recaptcha by passing g-recaptcha-response to php file from a javascript file 带Recaptcha v3的phpmailer不发送电子邮件-未定义索引:g-recaptcha-response - phpmailer with recaptcha v3 doesn't send email - Undefined index: g-recaptcha-response 谷歌reCaptcha - 如何获得g-recaptcha-response - 没有FORM TAG - Google reCaptcha - How to get g-recaptcha-response - without FORM TAG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM