1

Hey guys I am setting up user registration for my app, and the registration itself works fine. Like the email and password is saved in my Firebase authentication section, but I am trying to make it so a Username is stored as well. The code I have seems fine to me, but it won't create a Database in my Firebase with any saved user information like the username.

This is the code I am using to register the users.

        firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                //start profile activity here

                User user = new User(username, email);

                FirebaseDatabase.getInstance().getReference("Users")
                        .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                        .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                                    Toast.makeText(RegisterActivity.this, "Registration successful.", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(RegisterActivity.this, HomePage.class ));
                                } else {
                                    Toast.makeText(RegisterActivity.this, "Database not created", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
            } else {
                Toast.makeText(RegisterActivity.this, "Registration not successful, please try again.", Toast.LENGTH_SHORT).show();
                progressDialog.hide();
            }
        }
    });

When I try to register an account it says "Database not created" so I know that task.isSuccessful() is not true and I am not sure why that is.

Any help?

brent
  • 131
  • 12
  • [**`task.getException()`**](https://github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/java/EmailPasswordActivity.java#L102) will help us both to see what's the actually error. – ʍѳђઽ૯ท Oct 17 '18 at 20:40
  • I used task.getException() and the exception is "Permission denied" – brent Oct 17 '18 at 20:48

2 Answers2

1

Change the rules to:

{
 "rules": {
   ".read": true,
   ".write": true
  }
}
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Hi, thanks for the response. I am very new to Firebase so sorry if I am having troubles with simple thing! But I went into my Rules in Firebase and saw this:: service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if false; } } } So is changing the false to a true all I need to do? Or do I need to put the code you showed me somewhere in here? – brent Oct 17 '18 at 20:54
  • It's fine, no need to apologize! Check this https://stackoverflow.com/questions/52092843/permission-denied-to-access-firebase-datastore/52092889#52092889 since I think you are using realtime database. The rules that you provided in the comments section are for firestore which is another service. – Peter Haddad Oct 17 '18 at 20:58
  • This is a **security breach** and it's actually a good idea **for only testing purposes**. But you didn't consider the release build side. – ʍѳђઽ૯ท Oct 17 '18 at 20:59
  • 1
    Thank you this fixed my issue! I will look into alternative methods in the future so this doesn't hinder the security of my app but for now it's perfect! – brent Oct 17 '18 at 21:02
0

I used task.getException() and the exception is "Permission denied"

Which means you are not authenticated to the Firebase. You can make the Firebase rules open to everyone:

{
 "rules": {
   ".read": true,
   ".write": true
  }
}

Which is not a good idea for security.

By default, read and write access to your database is restricted so only authenticated users can read or write data. To get started without setting up Authentication, you can configure your rules for public access. This does make your database open to anyone, even people not using your app, so be sure to restrict your database again when you set up authentication. https://firebase.google.com/docs/database/android/read-and-write

Or, authenticate with the registered user by login the user in then do your setValue() or anything on the database.

Use signInWithEmailAndPassword.

Read: Android Firebase sign in with email and password

And sample.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • So, I have my initial problem solved of the registration failing due to the permission being denied. But now that I have made it public, which hinders the security of the app like you said, it allows the users to register with a username as well as their email. So is there a safer way to let users sign in with a username and have it saved into the Firebase Database? – brent Oct 17 '18 at 21:07
  • Yes. That's why I added the best way actually in the second part of my answer. Check [this answer](https://stackoverflow.com/a/37477852/4409113). You shouldn't change the rules actually since you're going to release the app too so, you'll need to authenticate the users again and the answer you accepted **is just a workaround**. But, the second part of my answer is your actuall answer to solve all issues. Also, set the rules to when users authenticated like [this answer's](https://stackoverflow.com/a/37477852/4409113) rules. – ʍѳђઽ૯ท Oct 17 '18 at 21:11