【发布时间】:2021-08-21 08:17:11
【问题描述】:
您好,我现在正在使用本机反应。 我一直面临这个问题
我想在不创建新用户的情况下使用电话验证。
但我找不到我想要的。 我尝试使用 npm @react-native-firebase/auth 和 firebase auth 我找不到它。 非常感谢您的帮助。
【问题讨论】:
标签: javascript firebase react-native firebase-authentication
您好,我现在正在使用本机反应。 我一直面临这个问题
我想在不创建新用户的情况下使用电话验证。
但我找不到我想要的。 我尝试使用 npm @react-native-firebase/auth 和 firebase auth 我找不到它。 非常感谢您的帮助。
【问题讨论】:
标签: javascript firebase react-native firebase-authentication
我想在不创建新用户的情况下使用电话验证。
电话号码验证内置在 Firebase 中,作为创建/验证用户的一种方式。如果不在 Firebase 身份验证中创建用户帐户,就无法使用 Firebase 的电话号码验证。
【讨论】:
Firebase 支持本地测试电话号码。要进行本地测试,您必须确保在 Firebase 控制台上创建应用程序时添加了本地计算机 SHA1 调试密钥。
成功创建用户后,使用邮箱和密码(参见 Authentication/Usage/Email/Password 登录),使用 verifyPhoneNumber 方法发送验证码到用户的电话号码,如果用户输入正确的代码,则将电话号码链接到经过身份验证的用户的帐户。
{...}
import auth from '@react-native-firebase/auth';
export default function PhoneVerification() {
{...}
// Handle the verify phone button press
async function verifyPhoneNumber(phoneNumber) {
const confirmation = await auth().verifyPhoneNumber(phoneNumber);
console.log(confirmation);
}
// Handle confirm code button press
async function confirmCode() {
try {
const credential = auth.PhoneAuthProvider.credential(confirm.verificationId, code);
let userData = await auth().currentUser.linkWithCredential(credential);
setUser(userData.user);
} catch (error) {
if (error.code == 'auth/invalid-verification-code') {
console.log('Invalid code.');
} else {
console.log('Account linking error');
}
}
}
{...}
<Button
title="Verify Phone Number"
onPress={() => verifyPhoneNumber('ENTER A VALID TESTING OR REAL PHONE NUMBER HERE')}
/>
<Button title="Confirm Code" onPress={() => confirmCode()} />
{...}
}
输入电话号码(例如 +44 7444 555666)和测试代码(例如 123456)。 更多资料https://rnfirebase.io/auth/phone-auth
【讨论】:
confirmation 最终会变成{ "code": null, "error": null, "state": "verified", "verificationId": null }。在这种情况下,您需要立即构建 auth.PhoneAuthProvider.credential(null, null) 并将其链接到已登录的用户。详情请参阅react-native-firebase#1962 和react-native-firebase#2895。