我想将当前登录用户Uid保存/显示到Firebase数据库中。这是我的代码:
private EditText mEmail, mPassword;
private Button mLogin, mRegistration;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_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(DriverLoginActivity.this,DriverMapActivity.class);
startActivity(intent);
finish();
return;
}
}
};
mEmail = (EditText) findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mLogin = (Button) findViewById(R.id.login);
mRegistration = (Button) findViewById(R.id.registration);
mRegistration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(DriverLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(DriverLoginActivity.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("Drivers").child(user_id);
current_user_db.setValue(true);
}
}
});
}
});
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = mEmail.getText().toString();
final String password = mPassword.getText().toString();
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(DriverLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(DriverLoginActivity.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);
}
用户注册和用户登录模块可以正常运行,但是用户Uid无法保存/显示在Firebase数据库中。
基本上,我想创建像uber这样的应用,并按照youtube turorial和以下链接进行操作:
https://www.youtube.com/watch?v=5uDNNJyKehM&list=PLxabZQCAe5fgXx8cn2iKOtt0VFJrf5bOd&index=4
在此视频中,一切正常,但我的Uid未显示在数据库中。
答案 0 :(得分:0)
firebase中的默认数据库规则如下。 这些规则不允许任何人对您的数据库进行读写访问
{
"rules": {
".read": false,
".write": false
}
}
您必须按照以下说明更改数据库规则,以允许经过身份验证的用户读写。
{
"rules": {
".read": "auth!=null",
".write": "auth!=null"
}
}
请转到您的Firebase控制台并签出“数据库”的“数据库规则”标签