客户不在Firebase中实时存储数据库,但可以在身份验证中看到

时间:2018-12-31 20:35:30

标签: android firebase firebase-realtime-database

客户不在Firebase中实时存储数据库,但可以在身份验证中看到,这里是我的代码,请检查我的代码有什么问题

public class CustomerLoginActivity extends AppCompatActivity
{

   private EditText mEmail,mPassword;

   private Button mLogin,mRegisteration;

   private FirebaseAuth mAuth;
   private FirebaseAuth.AuthStateListener firebaseAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_login);

    mAuth = FirebaseAuth.getInstance();

    firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if(user!=null){

                Intent intent = new Intent(CustomerLoginActivity.this,MapActivity.class);
                startActivity(intent);
                finish();
                return;

            }

        }
    };




    mEmail = (EditText) findViewById(R.id.email);
    mPassword = (EditText) findViewById(R.id.password);

    mLogin = (Button) findViewById(R.id.login);
    mRegisteration = (Button) findViewById(R.id.registeration);

    mRegisteration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = mEmail.getText().toString();
            final String password = mPassword.getText().toString();
            mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (!task.isSuccessful()){
                        Toast.makeText(CustomerLoginActivity.this,"sign up error", Toast.LENGTH_SHORT).show();

                    }else{
                        String user_id = mAuth.getCurrentUser().getUid();
                        DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers").child(user_id);
                        current_user_db.setValue(true);
                    }
                }
            });
        }
    });

    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = mEmail.getText().toString();
            final String password = mPassword.getText().toString();
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {


                    if (!task.isSuccessful()){
                        Toast.makeText(CustomerLoginActivity.this,"sign in error", Toast.LENGTH_SHORT).show();

                    }

                }
            });
        }
    });


}

@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(firebaseAuthListener);
}

@Override
protected void onStop() {
    super.onStop();
    mAuth.removeAuthStateListener(firebaseAuthListener);
}
}

客户不在Firebase中实时存储数据库,但可以在身份验证中看到,下面是我的代码,请检查我的代码有什么问题

1 个答案:

答案 0 :(得分:0)

正如@FrankvanPuffelen所说,如果您所做的一切都正确,并且仍然看不到Firebase数据库上的数据,那么很可能您会丢失Firebase数据库中的数据规则。

这是没有安全性的,Firebase的允许读写规则如下:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

默认的Firebase规则设置为此:

{
  "rules": {
    ".read": false,
    ".write": false
  }
}

以下是仅允许经过身份验证的用户能够从数据库写入/读取的规则集:

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

您可以了解有关rules in Firebase database here的更多信息。

Here is the list of set of rules for different purposes in Firebase database