【问题标题】:FIrebase-OTP sent successfully but not verifying code in react native androidFIrebase-OTP 发送成功,但未在反应原生 android 中验证代码
【发布时间】:2021-04-07 10:40:03
【问题描述】:

我正在尝试使用 Firebase 在本机应用程序中设置 OTP 验证。 我遵循了 firebase docs phone auth 中给出的所有步骤。 根据代码 otp 成功发送但未验证。 如果我使用另一个设备电话号码,则验证代码,但如果我使用安装了应用程序的相同电话号码(该电话号码的应用程序和 sim 卡都在同一设备中),那么它会给出无效代码的响应

async function confirmCode() {
    try {
        let confirmation = await props.route.params.confirmation;
        await confirmation.confirm(verifyCode);
        alert("Successfully Verified!!")
        props.navigation.navigate('home')
    } catch (error) {
        console.log('Invalid code.');
    }
}

return (
    <View style={styles.body}>

        <TextInput style={styles.textInput} placeholder="Verification Number" keyboardType="phone-pad"
            onChangeText={(text) => { setVerifyCode(text) }} />
        <TouchableOpacity onPress={() => { confirmCode() }} style={styles.button}>
            <Text style={{ color: 'white', alignSelf: 'center', fontSize: 19, fontWeight: 'bold' }}>Verify Code</Text>
        </TouchableOpacity>
    </View>
)

}

【问题讨论】:

    标签: android firebase react-native firebase-authentication


    【解决方案1】:

    如果您尝试使用相同的设备和相同的号码,那么您将无法通过手动输入 OTP 代码来确认代码。

    这里我们必须使用 onAuthStateChanged 事件来验证用户。

    当使用相同编号的设备进行身份验证时,必须添加事件侦听器并在此侦听器中处理用户身份验证才能正常工作。

    例如:

    useEffect(() => {
        const unsubscribe = auth().onAuthStateChanged(async user => {
            if (user && user.uid) {
                await dispatch(userActions.setUpUserData(currentUser.payload));
                Toast(currentUser.message);
                mounted.current && setIsLoading(false);
                mounted.current && setShowPhoneAuth(false);
            }
        });
    
        return () => {
            unsubscribe();
        };
    }, [currentUser]);
    

    有关更多详细信息,请查看官方文档。 1 - https://rnfirebase.io/auth/phone-auth 2 - https://rnfirebase.io/auth/usage#listening-to-authentication-state

    【讨论】:

    • @ChaitraliSapre 很高兴为您提供帮助。
    猜你喜欢
    • 2020-05-11
    • 2020-07-22
    • 2020-03-21
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2017-12-18
    相关资源
    最近更新 更多