如何验证 Firebase 中不存在两个相同的用户名?

huangapple go评论71阅读模式
英文:

How can I verify that there are not two identical username in Firebase?

问题

regBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        regBtn.setVisibility(View.INVISIBLE);
        loadingProgress.setVisibility(View.VISIBLE);
        final String email = userEmail.getText().toString();
        final String password = userPassword.getText().toString();
        final String password2 = userPAssword2.getText().toString();
        final String name = userName.getText().toString();

        if (verificationUsername(name)) {
            showMessage("用户名已存在,请尝试使用其他用户名");
            regBtn.setVisibility(View.VISIBLE);
            loadingProgress.setVisibility(View.INVISIBLE);
        }
    }
});

public boolean verificationUsername(String equalto) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference.child("Users").orderByChild("username").equalTo(equalto);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                String TypeUser = dataSnapshot.child("username").getValue().toString();
                System.out.println(TypeUser + "用户名已存在");
                lola = dataSnapshot.toString();
                showMessage("用户名已存在,请尝试使用其他用户名");

                for (DataSnapshot issue : dataSnapshot.getChildren()) {
                    // 处理每个“issue”节点的内容
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
    return false;
}
英文:

In my app I have to verify that there are not more than two users with the same name. I tried it with the following code, but when it detects that the user who tries to register already exists, it appears a toast that indicates to change it.

The problem is that even if you know that name already exists and although the toast appears, the account is created without importing the imposed condition, thus creating two users with the same username.

How could I avoid that, that two users with the same name will be created?

regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
regBtn.setVisibility(View.INVISIBLE);
loadingProgress.setVisibility(View.VISIBLE);
final String email = userEmail.getText().toString();
final String password = userPassword.getText().toString();
final String password2 = userPAssword2.getText().toString();
final String name = userName.getText().toString();
if(verificationUsername(name)){
showMessage("ass");
regBtn.setVisibility(View.VISIBLE);
loadingProgress.setVisibility(View.INVISIBLE);
} 
public boolean verificationUsername (String equalto){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("Users").orderByChild("username").equalTo(equalto);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String TypeUser = dataSnapshot.child("username").getValue().toString();
if (dataSnapshot.exists()) {
System.out.println(TypeUser + "dssssssssssssssssssssssssddddddsssssssssssssssssssssss");
lola = dataSnapshot.toString();
showMessage("El nombre de usuario ya esta en uso, intenta con uno nuevo");
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do something with the individual "issues"
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return false;
}

答案1

得分: 1

首先,我建议您将用户名的大写形式存储起来,以便所有句子变为大写,因为Firebase Realtime对大小写敏感。

因此,数据库中的示例可能如下所示:

username: Ticherhaz
usernameUpperCase: TICHERHAZ

我更喜欢使用addListenerForSingleValueEvent,因为我们只想要一次性地读取。

public boolean verificationUsername (String equalto){
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference.child("Users").orderByChild("usernameUpperCase").equalTo(equalto.toUpperCase());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) { 
               //这意味着该用户名已经存在
            }
            else {
               //因此用户名尚不存在,我们在这里创建新用户名。
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
    return false;
}
英文:

First of all I would recommend you to store the usernameUpperCase for all sentences become uppercase because Firebase Realtime is case sensitive.

So example for database something look like this:

> username: Ticherhaz<br>
> usernameUpperCase: TICHERHAZ

I would prefer to use addListenerForSingleValueEvent because we want to read for once only.

 public boolean verificationUsername (String equalto){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(&quot;Users&quot;).orderByChild(&quot;usernameUpperCase&quot;).equalTo(equalto.toUpperCase());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { 
//that&#39;s mean this username already exist
}
else {
//so username not exist yet, we create here new username.
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return false;
}

huangapple
  • 本文由 发表于 2020年4月6日 11:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61052699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定