我真的遇到了这个问题。我正在尝试从realtimedatabase firebase中删除一些用户。我正在使用Firebase提供的电子邮件/密码身份验证。在我重新验证用户后,我尝试从db中删除数据。我从firebase收到权限被拒绝错误(com.google.firebase.database.DatabaseException:Firebase数据库错误:权限被拒绝)。为什么呢?
这是我的代码:
public void reAuthenticateUser(String password) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider
.getCredential(user.getEmail(), password);
user.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
handleExceptions(task.getException());
} else {
dialog.dismiss();
DBHelper.getInstance().deleteUser(user.getUid());//the problem is here
deleteUserAccount();
}
}
});
}
这是我的DBHelper类:
public class DBHelper {
private static DBHelper instance = null;
private FirebaseDatabase database;
private DatabaseReference ref;
private DBHelper() {
database = FirebaseDatabase.getInstance();
}
public static DBHelper getInstance() {
if (instance == null)
instance = new DBHelper();
return instance;
}
public void saveUserOnDB(String userId, String name, String email) {
ref = database.getReference().child("users");
ref.child(userId).setValue(new User(name, email));
}
public void deleteUser(String userId) {
ref = database.getReference().child("users");
ref.child(userId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(!task.isSuccessful()){
Log.e("TAG","onComplete",task.getException());
}
}
});
}}
Firebase规则:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
在调试模式下,一切正常,而且,当我将数据库规则切换为public时,数据将从db中删除。
答案 0 :(得分:0)
在请求删除用户数据后,您似乎正在删除用户的帐户:
DBHelper.getInstance().deleteUser(user.getUid());
deleteUserAccount(); // <== does this call FirebaseUser.delete()?
如果是这样,您可能正在创建一个竞争条件,其中删除帐户在异步数据删除完成之前生效。将调用移至deleteUserAccount()
进入完成侦听器以删除数据:
public void deleteUser(String userId) {
ref = database.getReference().child("users");
ref.child(userId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
deleteUserAccount(); // <== MOVE HERE
} else {
Log.e("TAG","onComplete",task.getException());
}
}
});
}