本文介绍了如何检查Firebase身份验证中是否已存在电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的android应用中使用了带电话号码的Firebase身份验证.但是,firebase不会像电子邮件密码身份验证那样提供不同的登录和注册功能.如何检查用户是否已经存在?

I have used the firebase authentication with phone number in my android app. But firebase doesn't give different functions for sign in and sign up like the email password authentication. How can I check if the user already exists?

推荐答案

在这种情况下,您可以做的是将每个注册用户的电话号码存储在Firebase数据库的phone节点中.

What you can do in this case is that, store the phone number of every signed up user in the phone node of your Firebase database.

然后,当通过电话号码对新用户进行签名时,您可以在电话节点中运行一次检查,检查电话号码是否存在.

Then when signing a new user from a phone number, you can run a check in your phone node, that wether the phone number exists or not.

要将电话号码存储在数据库中名为phone的节点中,可以使用如下代码:

To store the phone number in a node named phone in your database, you can use a code like this:

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential){
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                   // you may be using a signIn with phone number like this, now here you can save the phone number in your database
                 DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("phone");

                 ref.child(phoneNumber).setValue(phoneNumber);

                }
                else if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                        Toast.makeText(MainActivity.this, "OTP is incorrect", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

在上面的代码中,phoneNumber是您要注册的用户编号.另外,我使用了相同的标题名称和值,即phoneNumber本身.您可以使用名称或其他任何名称.

In the above code, phoneNumber is the number of the user you're signing up. Also I have used the heading name and value same, and that is, phoneNumber itself. You can use a name or anything else, you want.

现在,当您注册新用户时,应该使用以下代码在数据库的phone节点中运行检查.您可以在上面的代码中将实例添加到此新方法中.

Now when you're signing up a new user, you should run a check in your phone node in your database, using the following piece of code. You can add instance to this new method in the code above.

boolean checkForPhoneNumber(String number){
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

  ref.orderByChild("phone").equalTo(number).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists())
                          return true;
                        else
                          return false;
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

}

这篇关于如何检查Firebase身份验证中是否已存在电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:40