Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase Database Error: Permission denied

I am working on an android project that requires user email and pwd authentication. The details are stored in the firebase database.The problem occurs whenever I try logging in again with the email and password. In my logcat the error message is:

W/SyncTree: Listen at / failed: DatabaseError: Permission denied 

Take a look at my code below:

public class LoginUser extends AppCompatActivity {  private RelativeLayout relativeLayout;  private EditText et_email, et_password; private Button loginBtn;  private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener authStateListener; private DatabaseReference databaseReference;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_login_user);      mAuth = FirebaseAuth.getInstance();     databaseReference = FirebaseDatabase.getInstance().getReference();     databaseReference.keepSynced(true);      relativeLayout = (RelativeLayout) findViewById(R.id.activity_login_user);      et_email = (EditText) findViewById(R.id.emailField);     et_password = (EditText) findViewById(R.id.pwdField);     loginBtn = (Button) findViewById(R.id.loginBtn);      loginBtn.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View view) {             initLogin();         }     });      authStateListener = new FirebaseAuth.AuthStateListener() {         @Override         public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {             if (firebaseAuth.getCurrentUser() != null){                 initLogin();             }             else {                 startActivity(new Intent(LoginUser.this,RegisterFireBase.class));             }         }     };  }  @Override protected void onStart() {     super.onStart();     mAuth.addAuthStateListener(authStateListener); }  @Override protected void onStop() {     super.onStop();     if (mAuth != null){         mAuth.removeAuthStateListener(authStateListener);     } }  private void initLogin() {      String email = et_email.getText().toString().trim();     String pass = et_password.getText().toString().trim();      if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass)){         mAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {             @Override             public void onComplete(@NonNull Task<AuthResult> task) {                  checkForUser();              }         });     }     else {         Toast.makeText(this, "Some fields are empty", Toast.LENGTH_SHORT).show();     }  }  private void checkForUser() {      final String userId = mAuth.getCurrentUser().getUid();     databaseReference.addValueEventListener(new ValueEventListener() {         @Override         public void onDataChange(DataSnapshot dataSnapshot) {             if (dataSnapshot.hasChild(userId)){                  Intent loginIntent =  new Intent(LoginUser.this, FireProfile.class);                 loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                 startActivity(loginIntent);                  Snackbar.make(relativeLayout,"Log In Successful",Snackbar.LENGTH_LONG).show();              }         }          @Override         public void onCancelled(DatabaseError databaseError) {          }     }); } 

}

What could be causing this?

like image 237
Nzadibe Avatar asked Dec 02 '16 23:12

Nzadibe


People also ask

Why Permission denied in Firebase database?

Most likely this is because the Database of projects created in the new Firebase Console are readable/writeable only by users that are signed in using Firebase Authentication. Since you're not signing the user in from your code, the database denies you access to the data.


1 Answers

Possible reason could be : you dont have read and write access on your database. For enabling read and write access :

Go to firebase console and enable read and write operations on your database.

Firebase Console -> Database(develop) -> RULES

{   "rules": {     ".read": "true",     ".write": "true"   } } 
like image 112
abhishesh Avatar answered Sep 21 '22 23:09

abhishesh