我正在尝试使用Firebase进行用户身份验证,同时将用户的全名和电子邮件地址存储在与身份验证期间创建的用户UID链接的数据库中。在这里,我的数据库表名是Users
,我一直在尝试使用以下代码将我的用户信息与其UID链接。
使用以下代码,用户将注册,但不会形成任何关联的数据库。我在这做错了什么?提前感谢您的建议。
public class SignUpActivity extends AppCompatActivity implements View.OnClickListener {
EditText editTextFullName, editTextEmail, editTextPassword;
private FirebaseAuth mAuth;
FirebaseDatabase database;
DatabaseReference ref;
Users users;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
editTextFullName = (EditText) findViewById(R.id.editTextFullName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
database = FirebaseDatabase.getInstance();
ref = database.getReference("Users");
users = new Users();
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.buttonSignUp).setOnClickListener(this);
}
private void getValues() {
users.setFullName(editTextFullName.getText().toString());
users.setEmail(editTextEmail.getText().toString());
}
private void registerUser() {
final String fullname = editTextFullName.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
finish();
startActivity(new Intent(SignUpActivity.this, ProfileActivity.class));
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
getValues();
ref.child(""+ user +"").setValue(users);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(getApplicationContext(), "You are already registered", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonSignUp:
registerUser();
break;
}
}
}
答案 0 :(得分:1)
当您调用
时,您正在完成活动,然后才能写入数据库finish();
方法
在数据写入数据库之后,即
之后,发出意图 ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
getValues();
ref.child(""+ user +"").setValue(users);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
startActivity(new Intent(SignUpActivity.this, ProfileActivity.class));
finish();
像这样
答案 1 :(得分:1)
不要忘记执行者类,这是一个恰当的例子:
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// 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(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});
}
});
}
答案 2 :(得分:0)
您不需要值事件侦听器,因为您可以直接添加到数据库中:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
databaseReference.push().setValue(users);
并且您必须在数据库调用
后调用finish();
答案 3 :(得分:0)
启动Activity并在将值设置为数据库后调用finish(),如@Rohit建议的那样。
还有一个建议是;如果要使用UID保存数据,则必须在参考路径中进行设置,如下所示:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String UID = user.getUid(); //get UID of currently logged in user(new user in your case)
ref.child(UID).setValue(users); //this will give you expected structure
startActivity(new Intent(SignUpActivity.this, ProfileActivity.class));
finish();
}