我使用Firebase作为我的后端(Android,Java),我在从数据库中检索用户名时遇到问题,以避免重复数据。
这是我的数据库设置:
usernameTaken:
[ID generated by push()]:
Username: username
users:
UidFromRegistration:
Email: emailFromRegistration
Username: username
我需要检查新用户是否选择已注册的用户名。
首先,我尝试在没有push()
的情况下保存数据,但我无法保存。
这是我的代码:
private void checkUser() {
String username = mUsernameSingUp.getText().toString().trim();
mDatabase.child("takenUsernames").orderByChild("Username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChildren()) {
registerUser();
} else {
Toast.makeText(SignupActivity.this, "Username already taken", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void registerUser() {
//getting email, username and password from edit texts
String username = mUsernameSingUp.getText().toString().trim();
String email = mEmailSingUp.getText().toString().trim();
String password = mPasswordSingUp.getText().toString().trim();
//checking if email, passwords and username are empty
if (TextUtils.isEmpty(username)) {
Toast.makeText(SignupActivity.this, "Please enter username", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(SignupActivity.this, "Please enter email", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(SignupActivity.this, "Please enter password", Toast.LENGTH_LONG).show();
return;
}
//if the email, password and username are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//creating a new user
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if (task.isSuccessful()) {
createNewUser(task.getResult().getUser());
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
} else {
//display some message here
Toast.makeText(SignupActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
private void createNewUser(FirebaseUser userFromRegistration) {
String username = mUsernameSingUp.getText().toString().trim();
String emailReg = userFromRegistration.getEmail();
String userIdReg = userFromRegistration.getUid();
Map<String, String> newUser = new HashMap<>();
newUser.put("Username", username);
newUser.put("Email", emailReg);
mDatabase.child("users").child(userIdReg).setValue(newUser);
Map<String, String> usernameIsTaken = new HashMap<>();
usernameIsTaken.put("Username", username);
mDatabase.child("takenUsernames").push().setValue(usernameIsTaken);
}
@Override
public void onClick(View view) {
if (view == mSingUpBtn) {
checkUser();
}
if (view == mRegTv) {
//open login activity when user taps on the already registered textview
startActivity(new Intent(this, LoginActivity.class));
}
}
}
我在论坛中寻找解决方案,但我尝试的一切都失败了。
我更新了上面的代码。我想我已经结束了。
答案 0 :(得分:0)
我认为您需要在创建新用户之前查看是否已经使用了用户名,例如:
mDatabase.child("users").orderByChild("Username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChildren())
showError("Username already taken");
else
registerUser();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
})
你不需要节点takeUsernames; 我使用了一些“伪”代码,但我认为很清楚该怎么做。