【发布时间】:2016-07-29 11:37:54
【问题描述】:
我正在制作一个安卓应用程序。我首先使用电子邮件和密码登录用户,然后在 OnComplete 方法中,如果任务成功,我正在尝试访问数据库上的数据。但是每次我收到消息 Listen at / failed.
我的主要活动代码如下 -
private FirebaseAuth auth;
private EditText inputEmail, inputPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
}
setContentView(R.layout.activity_main);
inputEmail = (EditText) findViewById(R.id.txtMobile);
inputPassword = (EditText) findViewById(R.id.txtPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
auth = FirebaseAuth.getInstance();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// 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()) {
// there was an error
Log.e("Invalid","Invalid");
} else {
/*Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);
finish();*/
Firebase.getDefaultConfig();
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
Log.e("awesome",auth.getCurrentUser().getEmail());
}
Firebase firebase = new Firebase("https://test-fd3f2.firebaseio.com");
firebase.addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@SuppressWarnings("unchecked")
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
if(snapshot.getValue()!= null) {
String mobileNum = snapshot.getValue().toString();
Log.e("here", mobileNum);
}
//Log.e("Title: ",newPost.get("password").toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("Cancel",firebaseError.getCode()+"");
Log.e("Cancel",firebaseError.getDetails());
Log.e("Cancel",firebaseError.getMessage());
}
//... ChildEventListener also defines onChildChanged, onChildRemoved,
// onChildMoved and onCanceled, covered in later sections.
});
}
}
});
}
});
}
更新 - 我的firebase规则如下-
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
【问题讨论】:
-
到底是什么错误?
-
" W/SyncTree: Listen at / failed: FirebaseError: Permission denied "是确切的错误。我无法从数据库中读取任何数据。
-
我使用规则仅在 auth != null 时显示数据。登录后,我可以得到登录用户的电子邮件,但仍然显示权限被拒绝。
-
也许看看这个线程是否有帮助。 stackoverflow.com/questions/37477644/…
-
这肯定行得通,但我不希望任何人读取数据,这就是我想首先进行身份验证的原因。
标签: android firebase firebase-realtime-database firebase-authentication