firebase 重复用户名和自定义登录 [英] firebase duplicate username and custom sign-in

查看:21
本文介绍了firebase 重复用户名和自定义登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的 firebase 系统允许我使用 firebase createuser() 使用电子邮件和密码注册新用户,在 oncomplete 方法中,我还获取用于注册的详细信息,因此电子邮件和密码以及其他一些变量我创建诸如用户名和 DOB,并使用以下代码将它们保存到 firebase 数据库:

My current firebase system allows me to signup with a new user using firebase createuser() taking an email and password, In the oncomplete method I also take the details used to sign up so the email and password along with some other variables I create such as username and DOB and save them to the firebase db with the following code:

// ...database code above
DatabaseReference users = database.getReference("users"); 
User user = new User(username, password,DOB,email); 
users.push().setValue(user);

  1. 我现在需要在将用户名保存到数据库之前检查该用户名是否唯一,如果不是它要求另一个用户名
  2. 我还实现了一个系统,当用户登录时,它会检查字符串是电子邮件还是用户名,如果用户使用电子邮件登录,那么它将使用 firebase 的系统登录,但如果他们使用一个用户名,然后它必须检查 firebase 数据库以查看用户名是否存在,然后检查密码是否正确

我不知道如何执行上述任何操作,非常感谢任何帮助.我已经使用了 stackoverflow 搜索并意识到已经有人问过这个问题的帖子,我尝试了其中一些没有用的解决方案,因为每个人的数据库结构都不同

I have no idea how to do any of the above, any help is much appreciated thanks. I have used the stackoverflow search and realise there are posts with this question that has already been asked, I have tried a few of these solutions that didn't work because everyones database structure is different

推荐答案

现在让我专注于第一个问题.唯一用户名.

Let me focus on the first problem as of now. Unique Usernames.

在 Firebase 中进行更大检索的关键方面之一是构建数据.您的结构决定了您的应用程序功能的效率.

One of the key aspects of having a greater retrieval in Firebase is structuring data. How you structure determines how efficient is your application's functionality.

对于唯一用户名,我在您的 Firebase 数据库中建议了类似的内容.

For unique usernames, I have suggest something like this in your Firebase Database.

在上面的数据库结构中,我为可用的用户名创建了一个单独的节点(因为某些值是必需的).这样每次当我必须检查时,我就不必再检查 USERS 节点的其他详细信息.

In the above Database Structure, I have made a separate node for usernames that not available (true because some value is required). So that every-time when I have to check, I will not have to go through other details of the USERS node.

在您的应用程序中,您可以使用这种方式来存储用户名.

In your Application, you can use it this way to store Username.

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference takenNames = database.getReference("TakenUserNames");

.
.
.
//On Successful Registeration
.
takenNames.child(username).setValue(true);

在注册时,您可以像这样检索该值:

And while registering, you can retrieve that value like this:

public boolean doesNameExist(final String sUsername)
    {
theTakenNameRef = database.getReference("TakenUserName");
        theTakenNameRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.hasChild(sUsername))
                {
                    isTaken = true;
                }
                else if (!dataSnapshot.hasChild(sUsername))
                {
                    isTaken = false;
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(mContext, "Connection Error. Please try again in some time.", Toast.LENGTH_SHORT).show();
            }
        });

        return isTaken;
    }
.
.
.
.
.
.
//Calling it in method
String username = editText.getText().toString().trim().toUpperCase();
boolean exists = doesNameExist(username);
if(exists)
{
//Show Error
}
else
{
//Continue Registration
}
}

希望这能说明检查唯一用户名的效率.

Hope this clears on how efficiently unique username can be checked.

使用这种机制,你也可以把你的逻辑放在第二个问题上.

And using this mechanism, you can put your logic for the second question as well.

这篇关于firebase 重复用户名和自定义登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆