1

I'm currently working on a quiz app, where a user signs up with email and pw via firebase.

After the signup he shall pass a nickname which will be used in for a highscore.

So basically I want the app to add a these kind of user information, everytime a user signs up as shown in the attached picture.

enter image description here

In order to achieve this I used this code:

public class createNickname extends AppCompatActivity {
    private static final String TAG = "createNickname";

    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private EditText NicknameInput;
    private Button NicknameReg;
    private String Nickname, UserId;
    private FirebaseAuth mAuth;
    private String EmailEntry;
    private DatabaseReference myref = mFirebaseDatabase.getInstance().getReference();

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_nickname);

        NicknameInput = (EditText)findViewById(R.id.etNickname);
        NicknameReg = (Button)findViewById(R.id.btsetNickname);

        mAuth = FirebaseAuth.getInstance();

        myref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange( DataSnapshot dataSnapshot ) {
                String value = dataSnapshot.getValue(String.class);
                Log.d(TAG, "value is: " + value);
            }

            @Override
            public void onCancelled( DatabaseError databaseError ) {
                Log.w(TAG, "Failed to read value.", databaseError.toException());
            }
        });

        NicknameReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View view ) {
                Log.d(TAG, "onClick: Attempting to add object to database.");
                String newNick = NicknameInput.getText().toString();
                if (!newNick.equals("")) {
                    FirebaseUser user = mAuth.getCurrentUser();
                    UserId = user.getUid();
                    EmailEntry = user.getEmail();
                    myref.child("userlist").push().setValue(UserId);
                    myref.child("userlist").child(UserId).push().setValue(EmailEntry);
                    myref.child("userlist").child(UserId).push().setValue(0);

                    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
    }
 }

So the question is now, what did I do wrong? A user is created in the authentication window in firebase, but the database is just not updated.

Did I target the database correctly? How do u decided which database you want to target?

These are my firebase security rules: { "rules": { ".read": false, ".write": false } }

Regarding the database nothing is happening if i get it right.

I get no error message what so ever.

Ingor Sievaz
  • 45
  • 10
  • What exactly is happening? What are your expectations and the actual results? Any errors? (In case of permission denied errors you should also post your security rules) I can probably help you but i need a bit more info. – André Kool May 24 '18 at 14:26
  • These are my firebase security rules: { "rules": { ".read": false, ".write": false } } Regarding the database nothing is happening if i get it right. Is there something that you need as well? Ahh end I get now error message what so ever – Ingor Sievaz May 24 '18 at 14:44
  • Can you include that information in your question? You can use the [edit](https://stackoverflow.com/posts/50511594/edit) button below your question. – André Kool May 24 '18 at 14:49
  • done, is there anything else to add? – Ingor Sievaz May 24 '18 at 14:56

1 Answers1

1

First I would advice to add a completion callback to your code so you know when and if your writes to Firebase have failed/succeeded.

In your case you would get a permission denied error because your security rules are set to false -> nobody has permission to read/write to your database. Instead you could give each user permission to write to their own data like this:

{
  "rules": {
    "userlist": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

(See the documentation for more information)

For the above to work you will also have to change your code you are using to write to the database. Currently you are using push(). This generates a random id and you don't need (want) that here. Instead you can use setValue():

myref.child("userlist").push().setValue(UserId);

myref.child("userlist").child(UserId).child("email").setValue(EmailEntry);
myref.child("userlist").child(UserId).child("highscore").setValue(0);

You can also look at my answer here for a bit more information about this subject.

André Kool
  • 4,880
  • 12
  • 34
  • 44