const [confirmResult, setConfirmResult] = useState(null)
const [phoneNumber, setPhoneNumber] = useState("")
const [showOtpInput, setShowOtpInput] = useState(false)
const [user, setUser] = useState()
已处理身份验证
useEffect(() => {
const unsubscribe = firebase.auth().onAuthStateChanged((user)=>{
if(user){
setUser(user)
}
})return ()=>unsubscribe()})
提交电话号码
const onSubmit = () => {
let pnum = "+91${phoneNumber}"
firebase.auth().signInWithPhoneNumber(pnum)
.then(response => {
setConfirmResult(response)
setShowOtpInput(true)
})
.catch(error => (console.log("Error :",error),alert(stringConstants.otpLimitReached)))
}
输入otp并确认otp是否正确以及进一步的代码,但是 真正的问题是,如果我在测试设备上收到代码,则说代码已过期,但是如果我测试其他号码,则该代码对其他设备同样有效
const confirmationCode = (otp) => {
confirmResult?.confirm ?
confirmResult.confirm(otp)
.then(() => {
*code here*
}))
})
.catch(error => {setShowOtpInput(false)}) : null}
查看:此处正在渲染所有内容
return (<ScrollView>
<OtpModal visible={showOtpInput} close={()=>setShowOtpInput(false)}
submitOtp={(otpVal)=>confirmationCode(otpVal)}/>
<TextInput value={phoneNumber} onSubmitEditing={onSubmit}returnKeyType={"done"}
onChangeText = {(phoneNumber)=>setPhoneNumber(phoneNumber)}/>
<TouchableOpacity onPress={()=> showOtpInput ? confirmationCode() : onSubmit()}>
<Text>{strings('LOGIN',language)}</Text>
</TouchableOpacity>
</ScrollView>)