Firebase phone 认证无需等待验证码
Firebase phone authentication without waiting for verification code
我正在开发一个具有 phone 用户注册身份验证的 React Native 应用程序。为此,我使用 firebase。问题是当用户输入 phone 号码并点击“发送代码”按钮时,firebase(Google) 成功通过短信向用户发送验证码,但在用户输入确认码之前并点击“确认”按钮,登录过程自动开始!!!为什么会这样?我该如何解决这个问题?
注:
- 如果使用测试 phone 数字(由 firebase 提供),则不会发生这种情况。
- 该应用没有读取用户短信内容的权限。
- 我在 firebase 应用程序设置中为调试和发布添加了 SHA-1 和 SHA-256 指纹
我的应用程序中的身份验证逻辑:
async function onSignUp() {
setLoading_signUp(true);
if (phonenumber.length === 10 && name !== '') {
try {
const confirmation = await auth().signInWithPhoneNumber(
'+98' + phonenumber
);
setConfirm(confirmation);
// console.log('confirm ===> ',confirm);
} catch (error) {
setLoading_signUp(false);
alert(error.message);
}
} else if (phonenumber.length !== 10 && name === '') {
setLoading_signUp(false);
alert('Invalid Phone number !\nName field should not be empty!');
} else if (phonenumber.length === 0) {
setLoading_signUp(false);
alert('Enter Phone number !');
} else if (0 < phonenumber.length < 10) {
setLoading_signUp(false);
alert('Invalid Phone number !');
} else {
setLoading_signUp(false);
alert('Name field should not be empty!');
}
}
async function confirmCode() {
setLoading(true);
try {
await confirm.confirm(code);
setConfirm(null);
await AsyncStorage.setItem('@DidSignUp', 'true');
await AsyncStorage.setItem(
'@credentials',
`{"name":"${name}","email":"${email}","phonenumber":"${
'+98 ' + phonenumber
}"}`
);
firestore().collection('users').doc(auth().currentUser.uid).set({
name,
email,
phonenumber,
});
} catch (error) {
setLoading(false);
if (error.code !== 'auth/unknown') {
alert('Invalid code !');
}
}
}
在 Android 上,Firebase Authentication SDK 实际上与 OS 一起工作以自动检测包含 OTP 的 SMS。这称为自动检索,并且 documented 为:
Auto-retrieval: on some devices, Google Play services can automatically detect the incoming verification SMS and perform verification without user action. (This capability might be unavailable with some carriers.) This uses the SMS Retriever API, which includes an 11 character hash at the end of the SMS message.
所以这是一个功能,而不是错误。 :)
似乎没有办法禁用该功能,据我所知:
我正在开发一个具有 phone 用户注册身份验证的 React Native 应用程序。为此,我使用 firebase。问题是当用户输入 phone 号码并点击“发送代码”按钮时,firebase(Google) 成功通过短信向用户发送验证码,但在用户输入确认码之前并点击“确认”按钮,登录过程自动开始!!!为什么会这样?我该如何解决这个问题?
注:
- 如果使用测试 phone 数字(由 firebase 提供),则不会发生这种情况。
- 该应用没有读取用户短信内容的权限。
- 我在 firebase 应用程序设置中为调试和发布添加了 SHA-1 和 SHA-256 指纹
我的应用程序中的身份验证逻辑:
async function onSignUp() {
setLoading_signUp(true);
if (phonenumber.length === 10 && name !== '') {
try {
const confirmation = await auth().signInWithPhoneNumber(
'+98' + phonenumber
);
setConfirm(confirmation);
// console.log('confirm ===> ',confirm);
} catch (error) {
setLoading_signUp(false);
alert(error.message);
}
} else if (phonenumber.length !== 10 && name === '') {
setLoading_signUp(false);
alert('Invalid Phone number !\nName field should not be empty!');
} else if (phonenumber.length === 0) {
setLoading_signUp(false);
alert('Enter Phone number !');
} else if (0 < phonenumber.length < 10) {
setLoading_signUp(false);
alert('Invalid Phone number !');
} else {
setLoading_signUp(false);
alert('Name field should not be empty!');
}
}
async function confirmCode() {
setLoading(true);
try {
await confirm.confirm(code);
setConfirm(null);
await AsyncStorage.setItem('@DidSignUp', 'true');
await AsyncStorage.setItem(
'@credentials',
`{"name":"${name}","email":"${email}","phonenumber":"${
'+98 ' + phonenumber
}"}`
);
firestore().collection('users').doc(auth().currentUser.uid).set({
name,
email,
phonenumber,
});
} catch (error) {
setLoading(false);
if (error.code !== 'auth/unknown') {
alert('Invalid code !');
}
}
}
在 Android 上,Firebase Authentication SDK 实际上与 OS 一起工作以自动检测包含 OTP 的 SMS。这称为自动检索,并且 documented 为:
Auto-retrieval: on some devices, Google Play services can automatically detect the incoming verification SMS and perform verification without user action. (This capability might be unavailable with some carriers.) This uses the SMS Retriever API, which includes an 11 character hash at the end of the SMS message.
所以这是一个功能,而不是错误。 :)
似乎没有办法禁用该功能,据我所知: