【发布时间】: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 有此方法的示例here:
【问题讨论】:
标签: javascript firebase next.js firebase-authentication