【问题标题】:react-native-firebase resend message codereact-native-firebase 重新发送消息代码
【发布时间】:2018-11-06 00:52:42
【问题描述】:

我正在寻找一种在react-native-firebase. 中重新发送短信验证码的方法

电话验证号码功能

   confirmPhone = async (phoneNumber) => {
    const phoneWithAreaCode = phoneNumber.replace(/^0+/, '+972');

    return new Promise((res, rej) => {
        firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
            .on('state_changed', async (phoneAuthSnapshot) => {
                switch (phoneAuthSnapshot.state) {
                    case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
                        res(phoneAuthSnapshot)

                        break

                    case firebase.auth.PhoneAuthState.CODE_SENT:
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        res(phoneAuthSnapshot)
                        break

                    case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT:
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        UserStore.setErrorCodeAuthentication('SMS code has expired')
                        res(phoneAuthSnapshot)


                    case firebase.auth.PhoneAuthState.ERROR:
                        if (phoneAuthSnapshot.error) {
                            this.showMessageErrorByCode(phoneAuthSnapshot.error.code)

                        }

                        rej(phoneAuthSnapshot)
                        break

                }
            })
    })
}

 confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
    try {
        const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code)
        UserStore.setCodeInput(code)
        UserStore.setUserCredentials(credential)
        AppStore.setAlreadyVerifiedAuto(true)
        await this.authenticate(credential)
        return credential
    } catch (e) {
        console.log(e)
        this.showMessageErrorByCode(e.code)
    }
}

我尝试重新发送短信的方法是在用户没有收到他的短信代码时再次调用 verifyPhoneNumber,但这不起作用,如果用户没有第一次收到短信或者他已经收到短信,我不会再次收到短信并且短信验证码已过期。

    resendeCode =  async()=>{
    const { AppStore, UserStore } = this.props
    try {
        AppStore.setLoading();
        UserStore.setErrorCodeAuthentication('')

        let { verificationId } = await FirebaseService.confirmPhone(phoneNumber); // call confirmPhone function above again

        this.props.UserStore.setVerificationId(verificationId)

        AppStore.setLoading();
    }catch(e){
        AppStore.setLoading();

    }
}

【问题讨论】:

    标签: javascript firebase react-native firebase-authentication


    【解决方案1】:

    要重新发送,只需将第二个参数添加为 true signInWithPhoneNumber("1234567891", true) 并通过重新发送再次调用此方法。 https://github.com/invertase/react-native-firebase/issues/721#issuecomment-403295962

    【讨论】:

    • 您好,我在“signIn”屏幕中使用了signInWithPhoneNumber,我将confirmResult 作为参数传递到特定屏幕“确认”中以现在在验证中使用它,我如何调用signInWithPhoneNumber 方法来重新发送代码?
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2019-02-22
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多