I need to give read/write access only to authenticated users, but it seems like Firebase is not recognizing that the user is authenticated.
After I sign in the user with email and password, I am assuming user is authenticated and should be allowed to read/write from database. However permission is denied.
Here is the code for sign in activity:
firebaseAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
//display some message here
// Toast.makeText(Register.this,"Successfully registered",Toast.LENGTH_LONG).show();
sRef.authWithPassword(email,password,new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
Toast.makeText(Register.this, user.getDisplayName()+" is authenticated",Toast.LENGTH_LONG).show();
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
}
});
Intent i = new Intent(Register.this,MainActivity.class);
startActivity(i);
}else{
//display some message here
Toast.makeText(Register.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
I want to use the default rule in firebase that allows only auth users to read/write from database:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
But when I change the rules as below my code is able to get read/write access to database.
{
"rules": {
".read": true,
".write":true
}
}
This is fine in testing, however I cannot continue with production with this method. Please help, how to authenticate a registered user to access database in Firebase.