【发布时间】:2017-01-27 18:54:27
【问题描述】:
我只需要向经过身份验证的用户授予读/写访问权限,但 Firebase 似乎无法识别用户已通过身份验证。
在我使用电子邮件和密码登录用户后,我假设用户已通过身份验证,并且应该被允许从数据库读取/写入。但是权限被拒绝。
这是登录活动的代码:
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();
}
}
});
我想在 firebase 中使用默认规则,只允许授权用户从数据库读取/写入:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
但是当我更改如下规则时,我的代码能够获得对数据库的读/写访问权限。
{
"rules": {
".read": true,
".write":true
}
}
这在测试中很好,但是我不能用这种方法继续生产。请帮助,如何验证注册用户以访问 Firebase 中的数据库。
【问题讨论】:
-
您共享的代码都没有访问数据库,因此它还不能引发该错误。请分享minimum, complete code that is needed to reproduce the problem。
-
您是否在登录前在 auth 部分启用了 firbase 登录方法,例如电子邮件/密码或匿名。
-
我没有分享访问 firebase 数据库的代码,因为它正在工作,但只有当我将规则更改为公共时。当我使用默认规则时,权限被拒绝。
-
@DhavalkumarSolanki 是的,我都做过。
标签: android firebase firebase-realtime-database firebase-security