Nza*_*ibe 6 android firebase firebase-authentication firebase-realtime-database
我正在开发一个需要用户电子邮件和密码认证的android项目.详细信息存储在firebase数据库中.每当我尝试使用电子邮件和密码再次登录时,就会出现问题.在我的logcat中,错误消息是:
W/SyncTree: Listen at / failed: DatabaseError: Permission denied
Run Code Online (Sandbox Code Playgroud)
看看下面的代码:
public class LoginUser extends AppCompatActivity {
private RelativeLayout relativeLayout;
private EditText et_email, et_password;
private Button loginBtn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_user);
mAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.keepSynced(true);
relativeLayout = (RelativeLayout) findViewById(R.id.activity_login_user);
et_email = (EditText) findViewById(R.id.emailField);
et_password = (EditText) findViewById(R.id.pwdField);
loginBtn = (Button) findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initLogin();
}
});
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null){
initLogin();
}
else {
startActivity(new Intent(LoginUser.this,RegisterFireBase.class));
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(authStateListener);
}
@Override
protected void onStop() {
super.onStop();
if (mAuth != null){
mAuth.removeAuthStateListener(authStateListener);
}
}
private void initLogin() {
String email = et_email.getText().toString().trim();
String pass = et_password.getText().toString().trim();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass)){
mAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
checkForUser();
}
});
}
else {
Toast.makeText(this, "Some fields are empty", Toast.LENGTH_SHORT).show();
}
}
private void checkForUser() {
final String userId = mAuth.getCurrentUser().getUid();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(userId)){
Intent loginIntent = new Intent(LoginUser.this, FireProfile.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
Snackbar.make(relativeLayout,"Log In Successful",Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
}
可能是什么导致了这个?
转到数据库控制台上的"规则"选项卡.如果您未明确授予.read访问权限,则会拒绝权限.
此链接在Firebase文档中非常出色:
https://firebase.google.com/docs/database/security/securing-data
该页面上的这两个注释特别令人感兴趣:
注意:默认情况下不允许访问.如果在路径上或路径上未指定.write或.read规则,则将拒绝访问.
注意:较浅的安全规则会覆盖较深路径的规则.子规则只能为父节点已声明的内容授予其他权限.他们无法撤销读取或写入权限.
查看拒绝权限的节点,并使用"规则"选项卡上的模拟器来测试不同用户安全上下文的规则(未经过身份验证,经过身份验证等)
可能的原因可能是:您没有数据库访问权限。启用读写访问权限:
转到firebase控制台,并在数据库上启用读写操作。
Firebase控制台->数据库(开发)->规则
{
"rules": {
".read": "true",
".write": "true"
}
}
Run Code Online (Sandbox Code Playgroud)
在Firebase数据库上进行一些更改。

{
"rules":
{
".read": true,
".write": true
}
}
Run Code Online (Sandbox Code Playgroud)
如果不需要,请勿将您的应用公开。如Google文档所述,您可以在Firebase>数据库>规则上执行以下规则
// These rules grant access to a node matching the authenticated
// user's ID from the Firebase auth token
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
或只允许经过身份验证的用户
// These rules require authentication
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
Run Code Online (Sandbox Code Playgroud)
让应用公开,任何人都可以编写和阅读您的应用...我认为任何应用都不应该这样使用。
| 归档时间: |
|
| 查看次数: |
22449 次 |
| 最近记录: |