[英]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.