【问题标题】:Prevent user enter same username and email to the database防止用户向数据库输入相同的用户名和电子邮件
【发布时间】:2017-08-22 07:21:50
【问题描述】:

目前我使用电子邮件身份验证将新用户注册到数据库,但现在我遇到了验证用户名和电子邮件的问题,这会阻止用户向数据库输入相同的用户名和密码,所以你们能帮我看看是否检索数据语句有错误?

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
    });

我希望可以防止用户在注册时输入相同的输入。

【问题讨论】:

  • 你想让那个使用已经退出吗?
  • 为什么不检查它的数据库程序或网络服务
  • 是的...使用按钮单击时检查数据库是否有相同的用户邮件和电子邮件。但它不起作用....

标签: java android firebase firebase-realtime-database


【解决方案1】:

为了解决您的问题,我建议您实施Firebase Authentication,这是您情况下的最佳解决方案。这种方法解决了您在身份验证方面的所有问题并节省了时间,因为您不需要维护任何代码。

如果您仍想使用您的方法,为了检查唯一性,我建议您稍微更改您的数据库结构。我建议您使用email address,而不是使用unique keyUID。它也是独一无二的,非常易于使用。请记住,使用 UID 并不总是最好的解决方案,因为如果您让用户可以删除他的帐户,如果他再次登录,则会生成另一个 UID,这会给您带来麻烦。

要检查电子邮件地址/用户名的唯一性,我建议您在 Firebase 数据库 (emailAddresses / userNames) 中创建另一个节点,您需要在其中添加所有电子邮件地址和所有用户名。要验证电子邮件地址/用户名是否存在,只需在该特定节点上添加一个侦听器并在 dataSnapshop 对象上使用 exists() 方法。

希望对你有帮助。

【讨论】:

  • 找到的解决方案是 ref.child(uid).orderByChild("username").equalTo(validateName).addValueEventListener(new ValueEventListener() 不过还是感谢你的建议
  • 你说得对,现在 uid 真的让我头疼
【解决方案2】:

这是在 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 下的用户名和电子邮件,以防止用户在数据库中输入相同的输入,所以您明白了吗?
  • 非常感谢您和您的解决方案。
  • 没问题的伙伴,您能否将答案视为已接受并投票。谢谢
  • 这个答案不能解决唯一性问题。使用 UID 并不总是最好的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多