Java 防止用户向数据库输入相同的用户名和电子邮件
目前我使用电子邮件身份验证将新用户注册到数据库,但现在我遇到了验证用户名和电子邮件的问题,这会阻止用户向数据库输入相同的用户名和密码,所以你们能帮我看看retrieve data语句是否出错吗Java 防止用户向数据库输入相同的用户名和电子邮件,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,目前我使用电子邮件身份验证将新用户注册到数据库,但现在我遇到了验证用户名和电子邮件的问题,这会阻止用户向数据库输入相同的用户名和密码,所以你们能帮我看看retrieve data语句是否出错吗 auth = FirebaseAuth.getInstance(); db = FirebaseDatabase.getInstance(); ref = db.getReference(); FirebaseUser user = auth.getCurrentUser();
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
ref = db.getReference();
FirebaseUser user = auth.getCurrentUser();
uid = user.getUid();//getuser id
run = (EditText) findViewById(R.id.Run);
rpw = (EditText) findViewById(R.id.Rpw);
rage = (EditText) findViewById(R.id.Rage);
//rbm = (RadioButton) findViewById(R.id.rbmale);
//rbfm = (RadioButton) findViewById(R.id.rbfemale);
re = (EditText) findViewById(R.id.Re);
rpn = (EditText) findViewById(R.id.Rpn);
ra = (EditText) findViewById(R.id.Ra);
rpc = (EditText) findViewById(R.id.Rpc);
//rbstd = (RadioButton) findViewById(R.id.rbstd);
//rbtt= (RadioButton) findViewById(R.id.rbtt);
final Button br = (Button) findViewById(R.id.Rbt);
br.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usnm = run.getText().toString().trim();
pswd = rpw.getText().toString().trim();
email = re.getText().toString().trim();
ref.child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object a = dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if (TextUtils.isEmpty(usnm)){
Toast.makeText(Register.this, "Username cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else if (usnm.length()<8){
Toast.makeText(Register.this, "Username cannot less then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (usnm.length()>8){
Toast.makeText(Register.this, "Username cannot greater then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (ref.child(uid).child("username").equals(usnm)){
Toast.makeText(Register.this, "Username had been taken already! Please try another one.", Toast.LENGTH_SHORT).show();
return;
}else if (TextUtils.isEmpty(pswd)){
Toast.makeText(Register.this, "Password cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else if (pswd.length()<8){
Toast.makeText(Register.this, "Password cannot less then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (pswd.length()>8){
Toast.makeText(Register.this, "Password cannot greater then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (TextUtils.isEmpty(email)){
Toast.makeText(Register.this, "Email cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else if (ref.child(uid).child("email").equals(email)){
Toast.makeText(Register.this, "Email had been taken already! Please try another one.", Toast.LENGTH_SHORT).show();
return;
}
register(usnm, pswd, email);
}//end of onclick
});
auth=FirebaseAuth.getInstance();
db=FirebaseDatabase.getInstance();
ref=db.getReference();
FirebaseUser=auth.getCurrentUser();
uid=user.getUid()//获取用户id
run=(EditText)findViewById(R.id.run);
rpw=(EditText)findViewById(R.id.rpw);
rage=(编辑文本)findViewById(R.id.rage);
//rbm=(单选按钮)findViewById(R.id.rbmale);
//rbfm=(单选按钮)findViewById(R.id.rbfemale);
re=(EditText)findViewById(R.id.re);
rpn=(EditText)findViewById(R.id.rpn);
ra=(EditText)findViewById(R.id.ra);
rpc=(EditText)findViewById(R.id.rpc);
//rbstd=(单选按钮)findViewById(R.id.rbstd);
//rbtt=(单选按钮)findViewById(R.id.rbtt);
最终按钮br=(按钮)findViewById(R.id.Rbt);
br.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
usnm=run.getText().toString().trim();
pswd=rpw.getText().toString().trim();
email=re.getText().toString().trim();
ref.child(uid).addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对象a=dataSnapshot.getValue();
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
if(TextUtils.isEmpty(usnm)){
Toast.makeText(Register.this,“用户名不能为空”,Toast.LENGTH_SHORT.show();
返回;
}else if(usnm.length()8){
Toast.makeText(Register.this,“用户名不能超过8个字符”,Toast.LENGTH_SHORT.show();
返回;
}else if(ref.child(uid).child(“用户名”).equals(usnm)){
Toast.makeText(Register.this,“用户名已被占用!请尝试另一个。”,Toast.LENGTH_SHORT.show();
返回;
}else if(TextUtils.isEmpty(pswd)){
Toast.makeText(Register.this,“密码不能为空”,Toast.LENGTH_SHORT.show();
返回;
}else if(pswd.length()8){
Toast.makeText(Register.this,“密码不能超过8个字符”,Toast.LENGTH_SHORT.show();
返回;
}else if(TextUtils.isEmpty(电子邮件)){
Toast.makeText(Register.this,“电子邮件不能为空”,Toast.LENGTH_SHORT.show();
返回;
}else if(参考child(uid).child(“电子邮件”).equals(电子邮件)){
Toast.makeText(Register.this,“电子邮件已被接收!请尝试另一封。”,Toast.LENGTH_SHORT.show();
返回;
}
注册(usnm、pswd、电子邮件);
}//onclick结束
});
我希望可以防止用户在注册过程中输入相同的输入。
以下是在Firebase上成功注册用户后获取uid的解决方案。您需要在注册时添加一个
onCompleteListener
,在Firebase返回uid后,您可以查询Firebase以获取用户名和电子邮件
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
//Problem with saving the data
if (databaseError != null) {
} else {
//User has registered, retrieve his uid
String uniqueKey = databaseReference.getKey();
}
}
以下是在Firebase上成功注册用户后获取uid的解决方案。您需要在注册时添加一个
onCompleteListener
,在Firebase返回uid后,您可以查询Firebase以获取用户名和电子邮件
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
//Problem with saving the data
if (databaseError != null) {
} else {
//User has registered, retrieve his uid
String uniqueKey = databaseReference.getKey();
}
}
为了解决您的问题,我建议您实施哪一个是您的最佳解决方案。这种方法解决了重新获得身份验证的所有问题,并节省了时间,因为您不需要维护任何代码 如果您仍然想使用您的方法,为了检查唯一性,我建议您稍微更改一下数据库结构。我建议您使用
电子邮件地址,而不是使用唯一键
或UID
。它也很独特,非常容易使用。请记住,使用UID并不总是最好的解决方案,因为如果您允许用户删除其帐户,如果他再次登录,将生成另一个UID,这将给您带来麻烦
为了检查电子邮件地址/用户名的唯一性,我建议您在Firebase数据库中创建另一个节点(电子邮件地址/用户名),您需要在其中添加所有电子邮件地址和所有用户名。要验证电子邮件地址/用户名是否存在,只需在该特定节点上添加一个侦听器,并在dataSnapshop
对象上使用exists()
方法
希望对您有所帮助。为了解决您的问题,我建议您实施对您来说最好的解决方案。这种方法解决了重新获得身份验证的所有问题,并节省了时间,因为您不需要维护任何代码
如果您仍然想使用您的方法,为了检查唯一性,我建议您稍微更改一下数据库结构。我建议您使用电子邮件地址,而不是使用唯一键
或UID
。它也很独特,非常容易使用。请记住,使用UID并不总是最好的解决方案,因为如果您允许用户删除其帐户,如果他再次登录,将生成另一个UID,这将给您带来麻烦
为了检查电子邮件地址/用户名的唯一性,我建议您在Firebase数据库中创建另一个节点(电子邮件地址/用户名),您需要在其中添加所有电子邮件地址和所有用户名。要验证电子邮件地址/用户名的存在性,只需在该特定节点上添加一个侦听器并使用exists