登录firebase身份验证的用户时,无法写入firebase数据库

时间:2018-01-23 15:58:51

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

我按照教程使用AuthUI.IdpConfig库来使用Firebase身份验证 -

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        Log.d(TAG, "Logged in");
    } else {
        Log.d(TAG, "Not logged in");
        // Choose authentication providers
        List<AuthUI.IdpConfig> providers = Arrays.asList(
                new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
                new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build());

// Create and launch sign-in intent
            startActivityForResult(
                    AuthUI.getInstance()
                            .createSignInIntentBuilder()
                            .setAvailableProviders(providers)
                            .build(),
                    RC_SIGN_IN);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        if (resultCode == RESULT_OK) {
            // Successfully signed in
            Log.d(TAG, "Successful sign in");
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            Log.d(TAG, "The token is " + FirebaseInstanceId.getInstance().getToken());
            // ...
        } else {
            Log.d(TAG, "Unsuccessful sign in");

            // Sign in failed, check response for error code
            // ...
        }
    }
}

我的问题是以下代码不会更新我的firebase数据库...

  FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");

    myRef.setValue("Hello, World!");

除非我先使用

退出
FirebaseAuth.getInstance().signOut();

我的规则设置为'true',用于读取和写入数据库。为什么登录会影响写入数据库?

1 个答案:

答案 0 :(得分:0)

你需要在setValue();

之前使用.child()或push()
//List data
myRef.push().setValue()

//Simple data
myRef.child("key").setValue()