node.js - 如何知道 reCAPTCHA v3 是否有效?

标签 node.js express vue.js vuetify.js captcha

我的前端使用 vuetify 如下:

   validate: async function () {
    let tokenCaptcha
    await this.$recaptcha('login').then((token) => {
      tokenCaptcha = token
    })

    if (this.$refs.form.validate() && tokenCaptcha) {
        let params = {
          birthDate: this.birthday,
          emailAddress: this.email,
          name: this.fullname,
          phoneNumber: this.phone,
          recaptcha: tokenCaptcha
        }
        this.modalSummary = true
        await this.setSummary(params) // Async vuex store / post to api backend
        if (this.dataSummary) {
          this.success = true
        } else {
          this.success = false
        }
    }
  }

引用:https://www.npmjs.com/package/vue-recaptcha-v3

我的后端/服务器端使用express js(node js),如下所示:

app.post('/summary', function (req, res) {
    if (req.body.recaptcha === undefined || req.body.recaptcha === '' || req.body.recaptcha === null) {
        res.send({success: false, msg: 'Please select captcha first'});
        return;
    }
    const secretKey = 'xxxxxx';
    const verificationURL = `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${req.body.recaptcha}&remoteip=${req.connection.remoteAddress}`;
    https.get(verificationURL, (resG) => {
        let rawData = '';
        resG.on('data', (chunk) => { rawData += chunk })
        resG.on('end', function() {
            try {
                var parsedData = JSON.parse(rawData);
                if (parsedData.success === true) {
                    let data = { 
                        date_of_birth: req.body.birthDate,
                        email_address: req.body.emailAddress,
                        full_name: req.body.name,
                        phone_number: req.body.phoneNumber,
                    }
                    let sql = "INSERT INTO summary SET ?";
                    let query = conn.query(sql, data,(err, results) => {
                        if(err) throw err;
                        res.send({success: "OK", message: 'Success', data: data});
                    });
                } else {
                    res.send({success: false, msg: 'Failed captcha verification'});
                    return;
                }
            } catch (e) {
                res.send({success: false, msg: 'Failed captcha verification from Google'});
                return;
            }
        });
    });
});

该代码有效。但如何知道 reCAPTCHA v3 是否有效?

因为版本 3 验证码没有出现。我只是想确定我的代码流程是否正确。除此之外,我想检查验证码是否出现,如果它是机器人

最佳答案

  1. 在用户界面(右下角)上,您应该看到:

enter image description here

  • 在后端:获取验证码响应的结果。喜欢:
  • console.info(parsedData)
    

    必须看起来像:

    {
      "success": true|false,      // whether this request was a valid reCAPTCHA token for your site
      "score": number             // the score for this request (0.0 - 1.0)
      "action": string            // the action name for this request (important to verify)
      "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
      "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
      "error-codes": [...]        // optional
    }
    

    ^ https://developers.google.com/recaptcha/docs/v3#site_verify_response

    关于node.js - 如何知道 reCAPTCHA v3 是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58898959/

    相关文章:

    node.js - 保证回调后执行aws lambda?

    javascript - 如何从 NodeJS 中的存储压缩文件 PDF

    vue.js - Vue - 将所有 Prop 保留在一个大型 mixin 中是否更好

    javascript - Object.keys() 从集合中返回 MongoDB 对象上的意外键

    node.js - 如何通过sequelize queryInterface插入关联行

    javascript - axios.post 未从服务器返回数据 : "Cannot destructure property ' data' of '(intermediate value)' as it is undefined"

    node.js - 使用 Jasmine 测试 Express/Passport 中间件——passport.authenticate 永远不会完成

    javascript - 无法读取未定义的属性 'send'

    vue.js - Vuetify RTL 风格

    javascript - Vue.js:当用户登录或未登录时,根据 Vuex Store 状态显示/隐藏导航栏上的按钮