我想通过身份验证在我的数据库中读取和写入,但我得到了两者的Permission denied
。我想弄清楚为什么它不起作用。
我使用了默认规则,因为我需要身份验证,如文档中所述。
The default rules require Authentication. They allow full read and write > access to authenticated users of your app. They are useful if you want data open > to all users of your app but don't want it open to the world.
Firebase 规则:
{ "rules": { ".read": "auth != null", ".write": "auth != null" } }
创建用户:
auth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d("USER_CREATE", "createUserWithEmail:onComplete:" + task.isSuccessful()); // 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()) { Toast.makeText(getApplicationContext(), "Authentication failed.", Toast.LENGTH_SHORT).show(); } else { startActivity(new Intent(SignupActivity.this, MainActivity.class)); finish(); } // ... } });
登录用户:
auth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d("COMPLETE", "signInWithEmail:onComplete:" + task.isSuccessful()); } });
我得到了一个 AuthListener
,它记录了我用户已登录。
授权监听器:
mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid()); } else { // User is signed out Log.d("TAG", "onAuthStateChanged:signed_out"); } // ... } }; // ...
Read test :(我在数据库中手动添加了一个节点)
firebase.child("test").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { Log.d("result", snapshot.getValue()+""); } @Override public void onCancelled(FirebaseError error) { Log.d("The read failed: ", error.getMessage()); } });
编写测试:我想要一个主节点
users
有两类用户:devs
和employers
.Firebase usersRef = firebase.child("users").child("devs"); FirebaseUser user = auth.getCurrentUser(); Map<String, String> userMap = new HashMap<String, String>(); userMap.put("name", "blabla"); userMap.put("city", "Brussels"); Map<String, Map<String, String>> users = new HashMap<String, Map<String, String>>(); users.put(user.getUid().toString(), userMap); usersRef.setValue(users);
日志:
D/AUTH LISTENER: onAuthStateChanged:signed_in:wnomkwiOWOb4wNNLSsrWeT5xxUq1 D/USER_CREATE: createUserWithEmail:onComplete:true I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead. D/ACTION BAR?: android.support.v7.app.ToolbarActionBar@36c3cd2a W/SyncTree: Listen at /test failed: FirebaseError: Permission denied D/The read failed:: Permission denied D/FirebaseAuth: Notifying listeners about user ( wnomkwiOWOb4wNNLSsrWeT5xxUq1 ). D/FirebaseAuth: Notifying listeners about user ( wnomkwiOWOb4wNNLSsrWeT5xxUq1 ). D/FirebaseApp: Notifying auth state listeners. D/FirebaseApp: Notified 0 auth state listeners. D/AUTH LISTENER: onAuthStateChanged:signed_in:wnomkwiOWOb4wNNLSsrWeT5xxUq1 D/FirebaseApp: Notifying auth state listeners. D/FirebaseApp: Notified 0 auth state listeners. D/AUTH LISTENER: onAuthStateChanged:signed_in:wnomkwiOWOb4wNNLSsrWeT5xxUq1 D/COMPLETE: signInWithEmail:onComplete:true
我在公开许可下进行了这 2 项测试(写入:true,读取:true)并且它正在运行,所以我的身份验证一定有问题。我可能误解/遗漏了一些东西,但并没有真正看到到底是什么。如果有人愿意启发我,那就太好了!
最佳答案
您将旧的 Firebase SDK 与新的 Firebase SDK 混合使用。从您的 build.gradle 中删除 firebase-android-sdk 2.5.2 并更新您的导入以使用新的 DatabaseReference
类而不是 Firebase
类。
关于android - 使用身份验证拒绝 Firebase 权限(读取和写入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38199100/