使用带Firebase的recaptcha

时间:2016-12-03 19:38:41

标签: firebase recaptcha

令人惊讶的是 - 当两者都是谷歌产品时更是如此 - 我可以找到关于如何将Recaptcha与Firebase集成的没有信息。它甚至可能吗?如果没有,我可以使用哪些内容在没有身份验证的Firebase应用上验证人类?

2 个答案:

答案 0 :(得分:10)

这是一篇非常古老的帖子,但这是像我这样的谷歌搜索者的答案。它现在已经内置,并且非常容易设置:

window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha', {
  'callback': (response) => {
    // reCAPTCHA solved, allow signInWithPhoneNumber.
    // ...
  },
  'expired-callback': () => {
    // Response expired. Ask user to solve reCAPTCHA again.
    // ...
  }
})
window.recaptchaVerifier.render()

正如tuananh所提到的,请务必添加<div id="recaptcha"></div>

答案 1 :(得分:5)

我刚刚发布了tutorial blog关于如何使用Firebase托管将reCAPTCHA集成到网站中以便为Firebase提供内容和云功能以验证从reCAPTCHA收到的响应的{{3}}。假设通过查询字符串接收响应令牌,函数本身看起来像这样:

const functions = require('firebase-functions')
const rp = require('request-promise')

exports.checkRecaptcha = functions.https.onRequest((req, res) => {
    const response = req.query.response
    console.log("recaptcha response", response)
    rp({
        uri: 'https://recaptcha.google.com/recaptcha/api/siteverify',
        method: 'POST',
        formData: {
            secret: 'PASTE_YOUR_SECRET_CODE_HERE',
            response: response
        },
        json: true
    }).then(result => {
        console.log("recaptcha result", result)
        if (result.success) {
            res.send("You're good to go, human.")
        }
        else {
            res.send("Recaptcha verification failed. Are you a robot?")
        }
    }).catch(reason => {
        console.log("Recaptcha request failure", reason)
        res.send("Recaptcha request failed.")
    })
})