我目前正在 React 应用程序中使用 Recaptcha 实现 Firebase 电话号码身份验证。但是,我需要绕过 Recaptcha 在非生产环境中进行自动化测试。以下是我正在使用的相关代码片段:
class Firebase {
constructor() {
const app = initializeApp(firebaseConfig);
this.getAuth = getAuth(app);
}
login(username) {
const appVerifier = window.recaptchaVerifier;
return signInWithPhoneNumber(getAuth(), username, appVerifier);
}
}
// on login click before otp verification
const login = async () => {
try {
await firebase
.login("+91" + phoneNumber)
.then((confirmationResult) => {
setCaptchaVerifier(confirmationResult);
})
.catch(function (error) {
console.error(error);
});
} catch (error) {
console.error(error);
}
};
//initialize captcha window
window.recaptchaVerifier = new RecaptchaVerifier("recaptcha-container", {
size: "invisible",
}, firebase.getAuth);
// on otp verfication
const verifyCode = () => {
if (!isValidVerificationCode()) {
setShowInvalidVerificationCode("show");
return;
}
captchaVerifier
.confirm(code)
.catch(function (error) {
console.error(error);
});
};
您可以指定电话号码进行测试,这样就不受配额影响并且(可能)绕过验证码。查看有关创建虚构电话号码和验证码的文档以了解更多信息。