跳过内容

开始

终端窗口
npm install @capgo/capacitor-recaptcha
npx cap sync

配置站点密钥

标题:配置站点密钥

在 Google Cloud reCAPTCHA 中创建平台密钥,然后添加它们到 capacitor.config.ts.

import type { CapacitorConfig } from '@capacitor/cli';
import '@capgo/capacitor-recaptcha';
const config: CapacitorConfig = {
appId: 'com.example.app',
appName: 'Example',
webDir: 'dist',
plugins: {
Recaptcha: {
androidSiteKey: 'ANDROID_SITE_KEY',
iosSiteKey: 'IOS_SITE_KEY',
webSiteKey: 'WEB_SITE_KEY',
enterprise: true,
},
},
};
export default config;

androidSiteKey, iosSiteKey,并 webSiteKey 覆盖共享 siteKey。您也可以传递一个 siteKey 直接到 load()execute() 当密钥取决于您的环境时。

import { Recaptcha } from '@capgo/capacitor-recaptcha';
const { token } = await Recaptcha.execute({
action: 'login',
});
await fetch('/api/recaptcha-assessment', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ token, action: 'login' }),
});

execute() 调用 load() 在客户端未准备好时,会自动调用,因此显式预加载步骤是可选的。

Web Standard reCAPTCHA v3

标题:Web Standard reCAPTCHA v3

设置 enterprise: false 以加载Google的标准Web reCAPTCHA v3脚本

const { token } = await Recaptcha.execute({
siteKey: 'WEB_V3_SITE_KEY',
enterprise: false,
action: 'signup',
});

在 Android 和 iOS 上,Google 的原生移动 SDK 路径仅限企业版/移动。传递 enterprise: false 在原生平台上会被拒绝,所以不会意外使用标准 Web v3 密钥。

插件接受旧 Cordova 选项别名 sitekeyAndroidsitekeyWeb 在调用选项和 Capacitor 配置中接受 sitekeyIossitekeyIOS 作为 iOS 迁移别名。prefer 使用 Capacitor 配置名称为新 code。

从 Getting Started 继续

标题:从 Getting Started 继续

如果您正在使用 Getting Started 来规划身份验证和帐户流程,连接它与 使用@capgo/capacitor-recaptcha 为本地能力在使用@capgo/capacitor-recaptcha中 @capgo/capacitor-social-login 为实现细节在@capgo/capacitor-social-login中 @capgo/capacitor-passkey 为实现细节在@capgo/capacitor-passkey中 @capgo/capacitor-native-biometric 为实现细节在@capgo/capacitor-native-biometric中,和 双因素身份验证 for the implementation detail in Two-factor authentication.