简体   繁体   English

PhoneAuthProvider.credential 不是函数 - Firebase V9 问题

[英]PhoneAuthProvider.credential is not a function - Firebase V9 Issue

I'm trying to use Firebase phone number verification with Next.js and Node.js.我正在尝试将 Firebase 电话号码验证与 Next.js 和 Node.js 结合使用。

This method is used to send code.此方法用于发送代码。

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);
        }
}

And when user enters the code, code and verificationId from above method is sent to backend.当用户输入代码时,上述方法中的代码和 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);
        }
    }

On the backend, I'm trying to use PhoneAuthProvider.credential method.在后端,我正在尝试使用 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 package has this method in the.d.ts files. Firebase 包在 .d.ts 文件中有这个方法。 在此处输入图像描述

Firebase has an example of this method here : Firebase 在这里有一个这种方法的例子: 在此处输入图像描述

But when the code is executed, I get this error in the console.但是当执行代码时,我在控制台中收到此错误。 在此处输入图像描述

You need to initialize the application before use.您需要在使用前初始化应用程序。 It would work if getAuth();它会工作,如果getAuth(); inserted before this line:在此行之前插入:
const credentials = PhoneAuthProvider.credential(verificationId, code);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 firebase v9 中使用 useCollection react-firebase 钩子 - How to use useCollection react-firebase hook with firebase v9 Firestore 集合到具有 Firebase v9 的对象数组 - Firestore collection to array of objects with Firebase v9 如何为 onSubmit function 从 firebase v9 获取/建立实时数据连接? - How to fetch/make realtime data connection from firebase v9 for the onSubmit function? 无法读取 firebase v9 {modular} 中未定义的属性(读取“indexOf”) - Cannot read properties of undefined (reading 'indexOf') in firebase v9 {modular} 如何从 firebase v9 获取服务器时间戳? - How to get server Timestamp from firebase v9? React Native Firebase v9 在配置上创建奇怪的错误 - React Native Firebase v9 Creating weird errors on configuration 如何在 javascript 上的 firebase v9 中获得授权用户 - How to get an authorized user in firebase v9 on javascript 如何在 React-native 中将我的 firebase v8 代码更新为 v9? - How do i update my firebase v8 code to v9 in React-native? 如何在 React Native 中从 Firebase v9 RT db 获取数据而不会导致无限循环 - How to get data from Firebase v9 RT db in React Native without causing infinite loop 登录后用户在刷新后注销 (React + Firebase v9) - Logged-In User is logged out after refresh (React + Firebase v9)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM