I want to read and write in my database with authentication but I got a Permission denied
for both. I'm trying to figure out why it's not working.
I used the default rules since I want authentication, as explained in the doc.
The default rules require Authentication. They allow full read and write > access to authenticated users of your app. They are useful if you want data open > to all users of your app but don't want it open to the world.
Firebase rules :
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
Create user :
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("USER_CREATE", "createUserWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
// ...
}
});
Sign in user :
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("COMPLETE", "signInWithEmail:onComplete:" + task.isSuccessful());
}
});
I got an AuthListener
and it logs me that the user is signed in.
AuthListener :
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("TAG", "onAuthStateChanged:signed_out");
}
// ...
}
};
// ...
Read test : (with a node I manually added in the database)
firebase.child("test").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Log.d("result", snapshot.getValue()+"");
}
@Override
public void onCancelled(FirebaseError error) {
Log.d("The read failed: ", error.getMessage());
}
});
Write test : I want a main node users
with two categories of users : devs
and employers
.
Firebase usersRef = firebase.child("users").child("devs");
FirebaseUser user = auth.getCurrentUser();
Map<String, String> userMap = new HashMap<String, String>();
userMap.put("name", "blabla");
userMap.put("city", "Brussels");
Map<String, Map<String, String>> users = new HashMap<String, Map<String, String>>();
users.put(user.getUid().toString(), userMap);
usersRef.setValue(users);
Logs :
D/AUTH LISTENER: onAuthStateChanged:signed_in:wnomkwiOWOb4wNNLSsrWeT5xxUq1
D/USER_CREATE: createUserWithEmail:onComplete:true
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
D/ACTION BAR?: android.support.v7.app.ToolbarActionBar@36c3cd2a
W/SyncTree: Listen at /test failed: FirebaseError: Permission denied
D/The read failed:: Permission denied
D/FirebaseAuth: Notifying listeners about user ( wnomkwiOWOb4wNNLSsrWeT5xxUq1 ).
D/FirebaseAuth: Notifying listeners about user ( wnomkwiOWOb4wNNLSsrWeT5xxUq1 ).
D/FirebaseApp: Notifying auth state listeners.
D/FirebaseApp: Notified 0 auth state listeners.
D/AUTH LISTENER: onAuthStateChanged:signed_in:wnomkwiOWOb4wNNLSsrWeT5xxUq1
D/FirebaseApp: Notifying auth state listeners.
D/FirebaseApp: Notified 0 auth state listeners.
D/AUTH LISTENER: onAuthStateChanged:signed_in:wnomkwiOWOb4wNNLSsrWeT5xxUq1
D/COMPLETE: signInWithEmail:onComplete:true
I made those 2 tests with public permission (write : true, read : true) and it was working, so something must be wrong with my authentication. I'm probably misunderstanding/missing something but don't really see what exactly. Would be very nice If someone would like to enlighten me!
You're mixing the old Firebase SDK with the new Firebase SDK. Remove firebase-android-sdk 2.5.2 from your build.gradle and update your imports to use the new DatabaseReference
class instead of the Firebase
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With