简体   繁体   English

如何检查特定用户名是否存在于 firebase 中? (Java 安卓)

[英]How to check if the particular username exists in the firebase? (Java Android)

I have tried all of the StackOverflow methods but all the methods are checking if it's null or not.我已经尝试了所有 StackOverflow 方法,但所有方法都在检查它是否为 null。 I wanted to check the particular strings under the "Users" node.我想检查“用户”节点下的特定字符串。 example: (aaaaa) (bbbbb) or the child value in it (username: "aaaaa") if users enter username as "asaaa" or "bbbbb" in registration will be a prompt error.例如:(aaaaa) (bbbbb) 或其中的子值(用户名:“aaaaa”) 如果用户在注册时输入用户名“asaaa”或“bbbbb”,将提示错误。 (i want to check all of the values in all of the nodes of firebase) I have searched all over the stack overflow but most of the solutions need to know the node names in order to run. (我想检查 firebase 所有节点中的所有值)我在整个堆栈溢出中进行了搜索,但大多数解决方案都需要知道节点名称才能运行。 I want to retrieve all of the names of the nodes under "Users" back to check.我想检索“用户”下的所有节点名称以供检查。 if a user uses "aaaaa" as a username during registration will be setError in the TextInputLayout.如果用户在注册期间使用“aaaaa”作为用户名,将在 TextInputLayout 中设置错误。

public class RegisterActivity extends AppCompatActivity {公共 class RegisterActivity 扩展 AppCompatActivity {

EditText username, fullname, email, password;
Button register;
TextView txt_login;
public String strUsername;

FirebaseAuth auth;
DatabaseReference reference;
ProgressDialog pd;
private static final String TAG = "RegisterActivity";

private static String users_from_database;
private ArrayList<String> username_list = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    username = findViewById(R.id.username);
    fullname = findViewById(R.id.fullname);
    email = findViewById(R.id.email);
    password = findViewById(R.id.password);
    register = findViewById(R.id.btn_Register);
    txt_login = findViewById(R.id.txt_login);


    auth = FirebaseAuth.getInstance();

    checkForUsername();

   txt_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(RegisterActivity.this,HomeActivity.class));
        }
    });

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pd = new ProgressDialog(RegisterActivity.this);
            pd.setMessage("Please wait...");
            pd.show();

            String str_username = username.getText().toString();
            String str_fullname = fullname.getText().toString();
            String str_email = email.getText().toString();
            String str_password = password.getText().toString();

            strUsername = username.getText().toString().trim();

            if (!username_list.contains(strUsername)) {
                // do your job here , suppose send verification code to phone number
                if(TextUtils.isEmpty(str_username) || TextUtils.isEmpty(str_fullname) || TextUtils.isEmpty(str_email) ||
                        TextUtils.isEmpty(str_password)){
                    Toast.makeText(RegisterActivity.this,"All fields are required!",Toast.LENGTH_SHORT).show();
                    pd.dismiss();
                } else if (str_password.length() < 6) {
                    Toast.makeText(RegisterActivity.this,"Password must be over 6 characters.",Toast.LENGTH_SHORT).show();
                    pd.dismiss();
                } else {
                    register(str_username,str_fullname,str_email,str_password);
                }
            } else {
                username.setError("Username Exist");
            }



        }
    });
}
private void checkForUsername(){

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users/");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {

                users_from_database = (String) ds.child("username").getValue();

                username_list.add(users_from_database);
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : username_list) {
                    stringBuilder.append(s + "\n");
                }
                Log.d("ZI", stringBuilder.toString());

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.d("ZI", "Failed");
        }
    });
}

private void register(final String username, final String fullname, String email, String password){
    auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){
                        FirebaseUser firebaseUser = auth.getCurrentUser();
                        String userID = firebaseUser.getUid();

                        firebaseUser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(RegisterActivity.this,"Verification Email Has Been Sent.", Toast.LENGTH_SHORT).show();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.d(TAG, "onFailure: Email not sent" + e.getMessage());
                            }
                        });


          

                        reference = FirebaseDatabase.getInstance().getReference().child("Users").child(username);

                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("username",username.toLowerCase());
                        hashMap.put("fullname",fullname);
                        hashMap.put("email",email);
                        hashMap.put("password",password);
                        hashMap.put("imageurl","");

                        reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if(task.isSuccessful()){
                                    pd.dismiss();
                                    Intent intent = new Intent(RegisterActivity.this,EmailActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                }
                            }
                        });
                    } else {
                        pd.dismiss();
                        Toast.makeText(RegisterActivity.this,"Register Failed",Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

} }

pic 2图 2

You can create a public path usernames where you just store the usernames that are already used and are publicly visible.您可以创建一个公共路径usernames ,您只需在其中存储已使用且公开可见的用户名。 When registering a new user you can check there is a username already exists.注册新用户时,您可以检查用户名是否已经存在。 Also don't try to get all of then to check if a single one exists.也不要试图让所有的 then 检查是否存在一个。 Just call the path usernames/{username} and if that is null there username doens't exist.只需调用路径usernames/{username} ,如果是null则用户名不存在。 Reading all of them would blow up your Firebase bill.阅读所有这些内容会破坏您的 Firebase 账单。

Yes you can do it by following steps.是的,您可以按照以下步骤进行操作。

  1. get all username child from your DatabaseReference and add it to a ArrayList.从您的DatabaseReference获取所有username child 并将其添加到 ArrayList。

  2. Check user provided username is available in ArrayList ( from step 1 ) or not.检查用户提供的username是否在 ArrayList(来自步骤 1 )中可用。 If contains then set error, otherwise do your job to complete registration.如果包含则设置错误,否则执行您的工作以完成注册。

Update- I think you are performing button click for registration before executing / finished checkForUsername();更新-我认为您在执行/完成checkForUsername(); method.方法。 So the if...else statement not working properly and its overriding data.所以if...else语句无法正常工作及其覆盖数据。 It was my mistake.这是我的错误。 We should perform check all users from database first then perform the next part of registration.我们应该先从数据库中检查所有用户,然后再进行下一部分注册。 So without using checkForUsername() method separately in onCreate() , we will do everything in button on click.因此,无需在onCreate()中单独使用checkForUsername()方法,我们将在单击按钮时执行所有操作。 check the code -检查代码 -

       register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pd = new ProgressDialog(LoginActivity.this);
            pd.setMessage("Please wait...");
            pd.show();

            String str_username = username.getText().toString();
            String str_fullname = fullname.getText().toString();
            String str_email = email.getText().toString();
            String str_password = password.getText().toString();

            strUsername = username.getText().toString().trim();


            DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users/");
            ref.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {

                users_from_database = (String) ds.child("username").getValue();

                username_list.add(users_from_database);
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : username_list) {
                    stringBuilder.append(s + "\n");
                }
              //  Log.d("ZI", stringBuilder.toString());

                  if (!username_list.contains(strUsername)) {
                    // do your job here , suppose send verification code to phone number
                    if (TextUtils.isEmpty(str_username) || TextUtils.isEmpty(str_fullname) || TextUtils.isEmpty(str_email) ||
                            TextUtils.isEmpty(str_password)) {
                        Toast.makeText(LoginActivity.this, "All fields are required!", Toast.LENGTH_SHORT).show();
                        pd.dismiss();
                    } else if (str_password.length() < 6) {
                        Toast.makeText(LoginActivity.this, "Password must be over 6 characters.", Toast.LENGTH_SHORT).show();
                        pd.dismiss();
                    } else {
                        register(str_username, str_fullname, str_email, str_password);
                    }
                } else {
                    username.setError("Username Exist");
                    pd.dismiss();
                }

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.d("ZI", "Failed");
        }
    });

        }
    });

Update- you can check DataSnapshot exist or not.更新 - 您可以检查DataSnapshot是否存在。 If not exist, then you can set test value to start your database.如果不存在,那么你可以设置测试值来启动你的数据库。 This will execute only one time.这只会执行一次。 You may check -你可以检查 -

       register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pd = new ProgressDialog(LoginActivity.this);
            pd.setMessage("Please wait...");
            pd.show();

            String str_username = username.getText().toString();
            String str_fullname = fullname.getText().toString();
            String str_email = email.getText().toString();
            String str_password = password.getText().toString();

            strUsername = username.getText().toString().trim();


            DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users/");
            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    if (!snapshot.exists()) {
                        HashMap<String, Object> testHash = new HashMap<>();
                        testHash.put("username", "testusername");
                        ref.child("test")
                                .setValue(testHash).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    ref.keepSynced(true);
                                    Toast.makeText(LoginActivity.this, "Please try again", Toast.LENGTH_SHORT).show();
                                    pd.dismiss();
                                } else {
                                    Log.d("ZI", "Failed ", task.getException());
                                }
                            }
                        });
                    } else {

                        for (DataSnapshot ds : snapshot.getChildren()) {
                            users_from_database = (String) ds.child("username").getValue();
                            username_list.add(users_from_database);
                            StringBuilder stringBuilder = new StringBuilder();
                            for (String s : username_list) {
                                stringBuilder.append(s + "\n");
                            }
                            Log.d("ZI", stringBuilder.toString());

                            if (!username_list.contains(strUsername)) {
                                // do your job here , suppose send verification code to phone number
                                if (TextUtils.isEmpty(str_username) || TextUtils.isEmpty(str_fullname) || TextUtils.isEmpty(str_email) ||
                                        TextUtils.isEmpty(str_password)) {
                                    Toast.makeText(LoginActivity.this, "All fields are required!", Toast.LENGTH_SHORT).show();
                                    pd.dismiss();
                                } else if (str_password.length() < 6) {
                                    Toast.makeText(LoginActivity.this, "Password must be over 6 characters.", Toast.LENGTH_SHORT).show();
                                    pd.dismiss();
                                } else {
                                    register(str_username, str_fullname, str_email, str_password);
                                }
                            } else {
                                username.setError("Username Exist");
                                pd.dismiss();
                            }

                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    Log.d("ZI", "Failed");
                }
            });

        }
    });

I have searched all over the stack overflow but most of the solutions need to know the node names in order to run.我搜索了整个堆栈溢出,但大多数解决方案都需要知道节点名称才能运行。

Even in your example, you can check if a user exists by checking the existence of the following node in the database:即使在您的示例中,您也可以通过检查数据库中是否存在以下节点来检查用户是否存在:

root/users/$userName

In code should look like this:在代码中应该是这样的:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
DatabaseReference userNameRef = usersRef.child("aaaaa");
userNameRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            if(snapshot.exists()) {
                String fullName = snapshot.child("fullName").getValue(String.class);
                Log.d("TAG", fullName);
            } else {
                Log.d("TAG", "The user doesn't exist!");
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

While @TarikHuber answer it will work, please note that in this case, it's not necessary to create an additional node to hold all usernames, since your database structure already does that.虽然 @TarikHuber 回答它会起作用,但请注意,在这种情况下,没有必要创建一个额外的节点来保存所有用户名,因为您的数据库结构已经这样做了。 So in terms of checking if a user exists:因此,就检查用户是否存在而言:

root/users/$userName

It's the exact same thing as:这与以下内容完全相同:

root/usernames/$userName

Since the nodes in the Realtime Database are represented by pairs of keys and values, please note that the keys are unique.由于实时数据库中的节点由键和值对表示,请注意键是唯一的。 So there is no way you can have duplicate nodes.所以你不可能有重复的节点。 As each node can be considered a Map.由于每个节点都可以被认为是一个 Map。

However, if by chance you can have multiple users in the database sharing the same user name, then you should use a query as in the following lines of code:但是,如果碰巧数据库中有多个用户共享相同的用户名,那么您应该使用以下代码行中的查询:

Query queryByUserName = usersRef.orderByChild("username").equalTo("aaaaa");
queryByUserName.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                if(ds.exists()) {
                    String fullName = ds.child("fullName").getValue(String.class);
                    Log.d("TAG", fullName);
                } else {
                    Log.d("TAG", "The user doesn't exist!");
                }
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

According to your screenshot, in both cases, the result in the logcat will be:根据您的屏幕截图,在这两种情况下,logcat 中的结果都是:

james

PS While @ZahidIslam answer might also work, downloading the entire "Users" node and performing the check on the client is not the best option, as it's considered very costly. PS 虽然@ZahidIslam 的回答也可能有效,但下载整个“用户”节点并在客户端执行检查并不是最佳选择,因为它被认为非常昂贵。 It's a waste of bandwidth and resources.这是对带宽和资源的浪费。

Edit:编辑:

According to you last comment:根据你最后的评论:

I think u misunderstood me.我想你误会我了。 I want to search thru all the nodes under the rootNodes我想搜索 rootNodes 下的所有节点

There is no way you can create a Query that can search under all nodes inside your database.您无法创建可以在数据库中的所有节点下进行搜索的查询。 You can, however, download the entire database and do the filtering on the client, but this is very costly and not recommended.但是,您可以下载整个数据库并在客户端进行过滤,但这样做成本很高,不推荐。

The best option that you have is to create a separate query for each and every node, but only if all the children have the same fields.您拥有的最佳选择是为每个节点创建一个单独的查询,但前提是所有子节点都具有相同的字段。 Or you can keep the users only in the "Users" node (as you do right now), and the code in my answer will always work perfectly fine.或者您可以将用户仅保留在“用户”节点中(就像您现在所做的那样),我的答案中的代码将始终运行良好。

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

相关问题 Android Firebase,如何检查文档中是否存在子集合 - Android Firebase, How to check if a subcollection exists within a document 如何从您的 Android 应用程序检查 Firebase 存储中是否存在文件? - How to check if a file exists in Firebase storage from your android application? 使用 Python 检查 FireBase 数据库中是否存在特定值 - Check If a Particular Value Exists in the FireBase Database Using Python 如何检查 firebase 数据库中是否存在唯一条目? - How to check if unique entry exists in firebase database or not? 如何检查Firebase认证中是否存在email? - How to check if an email exists in Firebase Authentication? 如何检查 Firebase 集合中是否已经存在特定 ID - How to check If particular Id already in Firebase collection Python Firebase 如何查看firebase存储上是否存在文件 - Python Firebase How to check whether a file exists on firebase storage 如何检查手机是否存在于 firebase auth - how to check if phone exists in firebase auth 如何查找 Firebase 中是否存在在其字段中具有特定价值的孩子? - How to find if a child with particular value in its fields exists in Firebase? 如何检查名称是否已存在于 Firebase 实时数据库(Firebase 和 JS)中 - How to check if name already exists in Firebase Realtime Database (Firebase & JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM