Firebase:使用Google登录的当前用户的updatePhoneNumber

时间:2018-08-22 13:00:49

标签: java android firebase firebase-authentication

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

问题:我从Google获得了显示名称电子邮件ID ,但没有得到电话号码。因此,我将在下一个活动中从用户那里获取该电话号码。

现在,如果我要将该电话号码更新为Firebase的当前用户,那么有什么方法呢?

我发现一种方法是 FirebaseAuth.getInstance().getCurrentUser().updatePhoneNumber() ,但没有任何适当的想法来使用它。

如果您实现了这件事,请帮助我。

感激之情。

谢谢。

4 个答案:

答案 0 :(得分:0)

FirebaseUser的updatePhoneNumber()方法:

  

更新用户的电话号码。

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

  

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

答案 1 :(得分:0)

一些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控制台的提供商中启用电话验证)

答案 2 :(得分:0)

我花了很多时间才知道该怎么做,但这是我的方法

  1. 用户firebase SDK验证
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
  })
  

答案 3 :(得分:0)

你可以在 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