使用 Firebase (MFA) 验证电话号码时出现问题

Tec*_*kie 5 javascript firebase firebase-authentication

I\xe2\x80\x99m 在注册多重身份验证时尝试使用 Firebase 验证电话号码。我有一个简单的 HTML 和 JavaScript 应用程序,我想在其中为已登录的用户启用 MFA。也就是说,用户未在登录\xe2\x80\x94 处注册 MFA,而是稍后通过其个人资料进行注册。I\xe2\x80\x99m 按照此处的说明进行操作:

\n

https://cloud.google.com/identity-platform/docs/web/mfa

\n

我可以成功初始化 RecapatchVerifier(步骤 3)并创建 multiFactorSession(步骤 4)并初始化 PhoneInfoOptions(步骤 5)。但是,当我尝试使用 PhoneAuthProvider 验证号码(第 6 步)时,它会抛出“auth/internal-error”。我认为这是因为我没有提供正确的电话选项(见下文)。phoneNumber 属性是否已弃用?我找不到任何其他不需要解析器提示的文档(\xe2\x80\x9c[s]使用第一个因素将用户登录,然后捕获身份验证/多因素身份验证-必需的错误。此错误包含解析器、已注册的第二个因素的提示以及证明用户已成功通过第一个因素进行身份验证的底层会话。\xe2\x80\x9d),我无法使用它,因为没有错误,我是第一次报名。

\n
function twoFactor(new_phone) {\n            console.log("got the phone number, start enrolling in 2FA\n");\n            multiFactor(user)\n                .getSession()\n                .then((multiFactorSession) => {\n                    console.log(\'1\')\n                    // Is this the right object to use when enrolling a new MFA?\n                    const phoneInfoOptions = {\n                        phoneNumber: new_phone,\n                        session: multiFactorSession\n                    }\n                    const phoneProvider = new PhoneAuthProvider(auth)\n                    return phoneProvider.verifyPhoneNumber(phoneInfoOptions, recapatchaVerifier) //error happens on the return\n                    // This promise creates an auth/internal-error\n                }).then(verificationId => {\n                    console.log(\'2\')\n                    const credential = PhoneAuthProvider.credential(verificationId, window.prompt(\'Enter your code\'))\n                    const assertion = PhoneMultiFactorGenerator.assertion(credential)\n                    return multiFactor(user).enroll(assertion)\n                })\n        }\n
Run Code Online (Sandbox Code Playgroud)\n

错误:

\n

在此输入图像描述

\n

编辑:

\n

添加.catch后如图所示:

\n
function twoFactor(new_phone) {\n        console.log(\'new_phone test:\', new_phone)\n        console.log(\'user test:\', user)\n        multiFactor(user)\n            .getSession()\n            .then((multiFactorSession) => {\n                console.log(\'1\')\n                // Is this the right object to use when enrolling a new MFA?\n                const phoneInfoOptions = {\n                    phoneNumber: new_phone,\n                    session: multiFactorSession  \n                }\n                console.log(\'phoneInfoOptions test:\', phoneInfoOptions)\n                console.log(\'auth test:\', auth)\n                const phoneProvider = new PhoneAuthProvider(auth)\n                \n                return phoneProvider.verifyPhoneNumber(phoneInfoOptions, recapatchaVerifier)\n                // This promise creates an auth/internal-error\n            }).then(verificationId => {\n                console.log(\'2\')\n                const credential = PhoneAuthProvider.credential(verificationId, window.prompt(\'Enter your code\'))\n                const assertion = PhoneMultiFactorGenerator.assertion(credential)\n                return multiFactor(user).enroll(assertion)\n            }).catch(err => console.log(err, err.message))\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n