【问题标题】:PhoneAuthProvider.credential is not a function - Firebase V9 IssuePhoneAuthProvider.credential 不是函数 - Firebase V9 问题
【发布时间】:2022-12-15 03:16:41
【问题描述】:

我正在尝试将 Firebase 电话号码验证与 Next.js 和 Node.js 结合使用。

此方法用于发送代码。

const sendVerificationCode = async () => {
        try {
            const appVerifier = window.recaptchaVerifier;
            const auth = getAuth();
            const confirmationResult = await signInWithPhoneNumber(auth, user.phoneNumber, appVerifier);
            setVerificationId(confirmationResult.verificationId);
            toast.success('Verification code sent to your phone');
        } catch (e) {
            toast.error(e.message);
        }
}

当用户输入代码时,上述方法中的代码和 verificationId 被发送到后端。

const submitVerificationCode = async (values) => {
        try {
            await axios.post('/users/verify-phone', {code: values.code, verificationId}, {headers: {Authorization: user.token}});
            toast.success('Your phone verified');
        } catch (e) {
            toast.error(e.message);
        }
    }

在后端,我正在尝试使用 PhoneAuthProvider.credential 方法。

exports.verifyPhone = async (req, res, next) => {
    try {
        const {verificationId, code} = req.body;

        const credentials = PhoneAuthProvider.credential(verificationId, code);

        const {user: {uid}} = await signInWithCredential(credentials)

        await User.updateById(uid, {phoneVerified: true})

        res.status(200).json({message: "Phone verified successfully!"})
    } catch (e) {
        next(e)
    }
} 

Firebase 包在 .d.ts 文件中有这个方法。

Firebase 有此方法的示例here

但是当执行代码时,我在控制台中收到此错误。

【问题讨论】:

    标签: javascript firebase next.js firebase-authentication


    【解决方案1】:

    您需要在使用前初始化应用程序。如果在这一行之前插入 getAuth();,它就会工作:
    const credentials = PhoneAuthProvider.credential(verificationId, code);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2022-08-17
      • 1970-01-01
      • 2021-11-05
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多