Firebase android : make username unique

前端 未结 3 1534
不思量自难忘°
不思量自难忘° 2020-11-21 22:43

Parse will shut down at the end of the year, so I decided to start using Firebase. I need to implement a register process with 3 fields : email, username, password (

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 23:20

    I dont know much about firebase security yet, but I may have solved the problem using Java. I have posted it below.

    my data structure is

    myapp
    {
      users: {
              
              { username: "example.username" }
    }
    }
    
    
    public boolean isUsernameExists(final String enteredUsername) {
            final Boolean[] isExist = {false};
            FBref.child("users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        String existingUsername = (String) userSnapshot.child("userName").getValue();
                        if (existingUsername.equals(enteredUsername)) {
                            isExist[0] = true;
                        }
                    }
                }
                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    //some error thrown here
                }
            });
            return isExist[0];
        }
    

提交回复
热议问题