使用电话号码登录Firebase

时间:2018-07-31 15:34:19

标签: android firebase firebase-authentication

如何通过获取现有用户的电话号码登录。我不想在登录时发送身份证明,因为它已经在注册时发送了

我想要注册用户 用户名和密码

Android所需的代码

3 个答案:

答案 0 :(得分:1)

phone-auth上登录firebase认为firebase数据库

// The test phone number and code should be whitelisted in the console.
String phoneNumber = "some_number";
String smsCode = "some_codes";

FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings();

// Configure faking the auto-retrieval with the whitelisted numbers.
firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode);

PhoneAuthProvider phoneAuthProvider = PhoneAuthProvider.getInstance();
phoneAuthProvider.verifyPhoneNumber(
  phoneNumber,
  60L,
  TimeUnit.SECONDS,
  this, /* activity */
  new OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(PhoneAuthCredential credential) {
      // Instant verification is applied and a credential is directly returned.
    }

    ... /* other callbacks */
  }

希望这会有所帮助。

答案 1 :(得分:1)

这意味着您需要用户输入密码。注册时,让用户创建密码或使用接收的代码作为密码并将其保存在firebase的节点中。在新登录期间,要求用户输入电话和密码,如果匹配,请登录用户。您的数据如下所示:

{
"login":{
        "+1254655826":"123235"
        "+4545492145":"password"
        "+2154644833":"passwordHASH"
    }
    //you can also hash the password
}

您现在要做的就是收听参考登录信息/电话,如果密码与用户输入的密码或注册过程中发送的密码匹配,则登录该用户。

有关电话身份验证,请参阅此doc on firebase

因此过程将会

reference.child("login/" + phone).addListenerForSingleValueEvent(new ->{
    if(dataSnapshot.getValue() != null){
        //user exists
        if(dataSnapshot.getValue().toString().matches(password)) //login successful

    }else  PhoneAuthProvider.getInstance()
        .verifyPhoneNumber(phone,60,TimeUnit.SECONDS,this,mCallbacks);
})

答案 2 :(得分:1)

对此没有解决方案,Google告诉人们使用该代码作为密码。您可以做的只有一个屏幕进行注册和登录。当FirebaseAuth.getInstance().getCurrentUser()返回null时,这意味着您必须以相同的方式注册/登录用户,这两个选项都需要一个只有电话号码所有者才能拥有的代码/密码。如果您确实要登录电话和密码,则可以执行类似this

的操作