使用带Firebase的recaptcha

Ser*_*lla 19 recaptcha firebase

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

Ala*_*ger 11

这是一篇非常古老的帖子,但这是像我这样的谷歌搜索者的答案.它现在内置,超级易于设置:

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()
Run Code Online (Sandbox Code Playgroud)

正如tuananh提到的,请确保添加一个<div id="recaptcha"></div>.

  • 我只想指出,此方法使用“firebase.auth”,这对服务器端身份验证或 node.js 实现无效。它只是客户端 javascript 项目的“内置”。Doug Stevenson 的上述方法对于任何进行服务器端渲染的人来说都是必要的。例如,如果您希望 firebase 函数在将用户输入发送到另一个函数之前验证验证码,以使用管理 SDK 创建新用户帐户。 (2认同)

Dou*_*son 6

我刚刚发布了一个教程博客,介绍如何使用Firebase Hosting为网站集成reCAPTCHA,以便为Firebase提供内容和云功能,以验证从reCAPTCHA收到的响应.假设通过查询字符串接收响应令牌,函数本身看起来像这样:

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.")
    })
})
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用任何您喜欢的HTTP客户端库,只要您遵循reCAPTCHA API的规范即可.你也可以随时做到. (2认同)