react-native-firebase重新发送消息代码

时间:2018-05-27 05:19:08

标签: javascript firebase react-native firebase-authentication

我正在寻找在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();

    }
}

1 个答案:

答案 0 :(得分:0)

要重新发送,只需在第二个参数中添加true signInWithPhoneNumber(“ 1234567891”,正确) 然后重新发送此方法。 https://github.com/invertase/react-native-firebase/issues/721#issuecomment-403295962