失败:FirebaseError:每次都拒绝权限

时间:2016-09-15 07:02:02

标签: android firebase firebase-realtime-database firebase-authentication

我试图通过电子邮件和密码使用Firebase Auth服务。我想做的是:

  1. 登录用户
  2. 成功登录后,我想从Firebase数据库中获取数据。
  3. 获得日志[onAuthStateChanged:signed_in: "xyz"]。在那个日志之后每当我获得权限被拒绝并且也得到这个:

      

    [W / DynamiteModule:本地模块描述符类        com.google.firebase.auth not found]。

    我的数据库规则:

    {
      "rules": {
          "Person" :{
              ".read": "auth !== null",
              ".write": "auth !== null"
           }
      }
    }
    

    我的代码

    private FirebaseAuth auth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        auth = FirebaseAuth.getInstance();
        btnLogIn.setOnClickListener(onClickBtnLogIn);
    
        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());
    
                    final Firebase ref = new Firebase("https://firebaseapplink/Person");
                    ref.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Map<String, Object> userData = (Map<String, Object>) dataSnapshot.getValue();
                            Log.d("TAG", "User Data :" + userData);
                        }
                        @Override
                        public void onCancelled(FirebaseError firebaseError) {
                            Log.d("TAG", firebaseError.getMessage()); // Permission Denied
    
                        }
                    });
    
                } else {
                    Log.d("TAG", "onAuthStateChanged:signed_out");
                }
            }
        };
    }
    
    
    
          View.OnClickListener onClickBtnLogIn = new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            final String Email = edtEmail.getText().toString().trim();
            final String Password = edtPwd.getText().toString().trim();
    
            auth.signInWithEmailAndPassword(Email, Password)
                    .addOnCompleteListener(abc.this, new OnCompleteListener<AuthResult>()
                    {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task)
                        {
    
                            if (task.isSuccessful())
                            {
                                Log.d("SignIn", "task");
                            }
                            if (!task.isSuccessful()) 
                            {
                                Log.d("SignIn", "task fail");
                            }
                        }
                    });
        }
    };
    
    @Override
    public void onStart() {
        super.onStart();
        auth.addAuthStateListener(mAuthListener);
    }
    

2 个答案:

答案 0 :(得分:1)

您正在使用新的9.4.0 SDK中的旧版2.x SDK与FirebaseAuth混合数据库服务。它们不兼容。从构建依赖项中删除compile com.firebase:firebase-client-android:2.x.x,替换为编译com.google.firebase:firebase-database:9.4.0,并使code change需要使用FirebaseDatabase

答案 1 :(得分:0)

确保您在build.gradle文件中添加了依赖项。

compile 'com.google.firebase:firebase-auth:9.4.0'