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

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

问题描述

我目前的firebase系统允许我使用firebase createuser()接收电子邮件和密码来注册一个新用户。在oncomplete方法中,我还会使用注册的细节,以便将电子邮件和密码以及其他一些变量创建如用户名和DOB,并将它们保存到firebase数据库与下面的代码:

  // ...上面的数据库代码
DatabaseReference users = database.getReference(users);
用户用户=新用户(用户名,密码,生日,电子邮件);
users.push()。setValue(user);




  1. 在保存到数据库之前,我需要检查用户名是如果它不是要求另一个用户名

  2. 我也实现了一个系统,当用户登录时,它检查字符串是否是电子邮件或用户名,如果用户用电子邮件登录,然后使用firebase的系统登录,但如果他们使用用户名,那么它必须检查firebase数据库,看看用户名是否存在,然后检查密码是否正确。

我不知道如何去做以上的任何帮助,非常感谢。我已经使用了stackoverflow搜索,并认识到有这个问题,已经被问到的帖子,我已经尝试了一些这些解决方案,因为每个人的数据库结构不同,所以不工作

解决方案

让我专注于现在的第一个问题。 唯一用户名
$ b 在Firebase中进行更大检索的关键方面之一是构建数据。你的结构如何决定你的应用程序的功能有多高效。



对于唯一的用户名,我在Firebase数据库中提供了类似的建议。

not 可用的用户名做了一个单独的节点(true,因为 需要一些 值)。所以每次我必须检查的时候,我都不必再去看看 USERS 节点的其他细节。



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

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




//成功注册

takenNames.child(username).setValue(true);

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

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

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

return isTaken;
}






//在方法$ b $中调用它b字符串username = editText.getText()。toString()。trim()。toUpperCase();
boolean exists = doesNameExist(username);
if(exists)
{
//显示错误
}
else
{
//继续注册
}
}

希望能够清楚说明如何有效地检查唯一的用户名。 b
$ b

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

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. I need to check now before saving to the database that the username is unique if it is not it asks for another username
  2. I have also implemented a system where when the user logs in, it checks the String if it is an email or a username, if the user logs in with an email then it'll use firebase's system to login but if they use a username then it has to check the firebase database to see if the username exists and then checks if the password is correct

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.

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.

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

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天全站免登陆