0

I am using firebase firestore in my unity project. I did the registration part, but if a user registered with the name of sam, another user can also register with the name of sam. How can I prevent this? (I am using this code)

public void SaveUserData()
    {

        DocumentReference docRef = db.Collection("users").Document(uname.text);
        Dictionary<string, object> city = new Dictionary<string, object>
{
            { "name", uname.text },
            { "email", email.text },
            { "password", password.text },
            { "wallet", wallet.text },
            { "uid", auth.CurrentUser.UserId },
            { "score", 0 },
            { "timestamp", FieldValue.ServerTimestamp }
};
        docRef.SetAsync(city).ContinueWithOnMainThread(task => {
            Debug.Log("Added data to the LA document in the cities collection.");
            PlayerPrefs.SetString("currentUname", uname.text);
            SceneManager.LoadScene(1);
        });
    }
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 1
    Welcome to StackOverflow! Well in general: You check if such an entry already exists in your database and if yes then throw an error and tell the new user to use a different name? – derHugo Jun 02 '21 at 09:14
  • You can refer to this answer https://stackoverflow.com/a/59892127/15803365 on a stackoverflow question that is based on indexing and deals with how to enforce uniqueness on a document field (other than document ID).This solution is about indexing on “username” as key and providing a value that contains the ID of the user that is indexed. Here in this solution we are setting up our Firestore rules so that they only allow a User to be created if the username is not already indexed for a different User.This might help you solve your issue of duplicate property (username) entry into the database. – Priyashree Bhadra Jun 04 '21 at 14:36
  • Also please use the correct tags! Note that [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long **deprecated** by now! – derHugo Jun 14 '21 at 07:58

1 Answers1

1

Just to extend derHugo right comment:

You can achieve this with multiple approaches, the first one is what derHugo says, check if any other User contains the name doing something similar to:

//Assuming that you are using client SDK and that your User class has a variable name called name
string COL_ID_USERS = "users";
Query usersQuery = db.Collection(COL_ID_USERS).WhereEqualTo(nameof(User.name) ,uname.text);
QuerySnapshot colQuerySnapshot = await usersQuery.GetSnapshotAsync();

if(colQuerySnapshot.Documents.Count() > 0)
{
    //Already exists the user   
}
else
{
    //Collection does not exists or is empty, so no user has this name
}

The second approach is to store all users name on a different document/collection, so every time you want to check, instead of query all users, just do a simple search on this document, basically to take advantage on how no-relational databases works.

Lotan
  • 4,078
  • 1
  • 12
  • 30