繁体   English   中英

Firebase:使用谷歌登录的当前用户的updatePhoneNumber

[英]Firebase : updatePhoneNumber for Current User who logged in with google

Firebase 开发人员,我在我的一个应用程序中使用 google 登录并成功完成。

问题:我从 Google 获取Display NameEmail Id ,但没有 getti get Phone Number 所以我在下一个活动中从用户那里获取该电话号码。

现在,如果我想将该电话号码更新为 Firebase 的当前用户,那么有什么方法可以做到这一点。

我发现了一种方法FirebaseAuth.getInstance().getCurrentUser().updatePhoneNumber()但没有得到任何正确的想法来使用它。

如果你已经实现了这件事,请帮助我。

赞赏提前。

谢谢你。

FirebaseUser 的updatePhoneNumber()方法:

更新用户的电话号码。

正如您所看到的,它将PhoneAuthCredential对象作为参数。 因此,为了更新相应用户的电话号码,请调用updatePhoneNumber()方法并将新的电话凭据对象作为参数传递。

重要提示:这是一项安全敏感操作,要求用户最近登录。如果不满足此要求,请要求用户再次进行身份验证,然后再调用reauthenticate(AuthCredential)

一些包含实现的 github 存储库:

https://github.com/wardah9/QuestionsQate/blob/9224a6d2e00a9304566be063c2d611b76cc76fb8/app/src/main/java/com/questionqate/StudentProfile/UpdatedMobileAthuntication.java

您需要导入并构建一个“com.google.firebase.auth.PhoneAuthCredential”

为此,您需要使用“com.google.firebase.auth.PhoneAuthProvider”要求您的用户使用他们的电话号码进行身份验证。

由于您使用的是 GoogleAuthProvider,因此您可以使用您发布并自己构建 PhoneAuthCredential 的方法“手动”更新用户电话号码,或者您需要创建一个新的 PhoneAuthProvider 并使已经通过身份验证的用户使用他的电话号码重新进行身份验证(您需要在 Firebase 控制台的提供商中启用电话身份验证)

我花了很多时间才知道如何做到这一点,但我是这样做到的

  1. 用户 firebase SDK recapcha
window.recaptchaVerifier = new fireabase.auth.RecaptchaVerifier('sign-in-button', {
  size: 'invisible'
})
  1. 发送短信代码
const phoneNumber = this.input.phone
const appVerifier = window.recaptchaVerifier
firebase.auth().currentUser.linkWithPhoneNumber(phoneNumber, appVerifier)
 .then((confirmationResult) => {
   window.confirmationResult = confirmationResult
   // prompt user to entre code 
   ...
})
 .catch((error) => {
   // reset rechatcha and try again 
   appVerifier.reset('sign-in-button')
   alert(error.message)
})

  1. 确认代码和链接
const code = this.input.code
window.confirmationResult.confirm(code).then((result) => {
  const credential = firebase.auth.PhoneAuthProvider.credential(window.confirmationResult.verificationId, code)
  firebase.auth().currentUser.linkWithCredential(credential)
})
  .then(() => {
    // done
  })
  .catch((error) => {
    alert(error.message)
   // try again
  })
  

你可以在 React Native 中做到这一点。

 handlePhone=()=>{
 const auth = firebase.auth();
 const {phoneNumber} = this.state;
 const self = this;
 if(phoneNumber != ''){
  try{
    const snapshot = await auth.verifyPhoneNumber(`+92${phoneNumber}`).on('state_changed',
      async (phoneAuthSnapshot) => {
        switch(phoneAuthSnapshot.state){
          case firebase.auth.PhoneAuthState.CODE_SENT:
            self.setState({verificationSnapshot:phoneAuthSnapshot,showOTP:true})
        }
      })
  }catch(error){
    console.log(error);
    this.showAlert('Try again later');
  }
}else{
  this.showAlert('Please enter phone number.');
}
}


handleVerifyOTP=()=>{
const {verificationCode, verificationSnapshot} = this.state;
    console.log(verificationCode, verificationSnapshot)
    const self = this;
    try{
        const  credential = await firebase.auth.PhoneAuthProvider.credential(verificationSnapshot.verificationId, verificationCode);
        console.log(credential)
        const u =  await firebase.auth().currentUser.updatePhoneNumber(credential);
        console.log(u)
        self.setState({showOTP:false,phoneNumberState:true});
        Alert.alert('Number has been registered successfully')
    }catch(error){
        Alert.alert('Something went wrong');
        console.log(error,"ERRR")
    }
    }

添加两个带按钮的输入字段 一个用于电话号码 另一个用于 OTP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM