我正在制作具有用户个人资料的类似社交媒体的应用。我想在使用他们的uid注册时保存他们的个人资料数据。虽然注册成功,但配置文件未保存在firebase数据库中。我还检查了规则,并为经过身份验证的用户设置了读写功能。
这是我的代码:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_reg;
private EditText etName, etEmail, etPassword, etCPassword, etMobile, etSchoolCompany, etLocation;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Firebase.setAndroidContext(this);
firebaseAuth = FirebaseAuth.getInstance();
progressDialog = new ProgressDialog(this);
btn_reg = (Button)findViewById(R.id.btnRegister);
etName = (EditText)findViewById(R.id.etName);
etEmail = (EditText)findViewById(R.id.etEmail);
etPassword = (EditText)findViewById(R.id.etPassword);
etCPassword = (EditText)findViewById(R.id.etCPassword);
etMobile = (EditText)findViewById(R.id.etMobile);
etSchoolCompany = (EditText)findViewById(R.id.etSchoolCompany);
etLocation = (EditText)findViewById(R.id.etLocation);
btn_reg.setOnClickListener(this);
}
@Override
public void onClick (View view){
if(view == btn_reg){
registerUser();
}
}
private void registerUser(){
String name = etName.getText().toString();
String mobile = etMobile.getText().toString();
String SchoolCompany = etSchoolCompany.getText().toString();
String location = etLocation.getText().toString();
final String email = etEmail.getText().toString().trim();
final String password = etPassword.getText().toString().trim();
String cpassword = etCPassword.getText().toString().trim();
progressDialog.setMessage("Registering..");
progressDialog.show();
//REGISTERING USER
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//THIS IS FOR STORING AUTHENTICATED USER'S DATA
final Firebase ref = new Firebase("https://myfirebase.firebaseio.com");
ref.authWithPassword(email, password, new Firebase.AuthResultHandler(){
@Override
public void onAuthenticated(AuthData authData){
// Authentication just completed successfully :)
Map<String, String> map = new HashMap<String, String>();
map.put("provider", authData.getProvider());
if(authData.getProviderData().containsKey("displayName")) {
map.put("displayName", authData.getProviderData().get("displayName").toString());
}
ref.child("users").child(authData.getUid()).setValue(map);
}
@Override
public void onAuthenticationError(FirebaseError error) {
//ERRORS TODO
}
});
progressDialog.hide();
Toast.makeText(RegisterActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RegisterActivity.this, "Registration failed, please try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 0 :(得分:4)
来自createUserWithEmailAndPassword method reference
尝试使用给定的电子邮件地址和密码创建新的用户帐户。 如果成功,它还会将用户签入应用程序。
这意味着您无需在注册过程完成后再次对用户进行身份验证。
要确保任务完成且firebaseUser
不为空,最好将AuthStateListener
添加到FirebaseAuth
,而不是在onComplete
方法中保存数据,代码在the official guide
然后,如果用户通过身份验证,您可以保存数据。
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map<String, String> map = new HashMap<>();
map.put("provider", user.getProviders().get(0));
if(user.getProviderData().containsKey("displayName")) {
map.put("displayName", user.getProviderData().get("displayName").toString());
}
ref.child("users").child(user.getUid()).setValue(map);
}
}
答案 1 :(得分:2)
假设您要在数据库中添加用户名,请编写
String name = etName.getText().toString().trim();
然后只需创建DatabaseReference
类的对象,例如:
DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef = myRootRef.child("User Name");
userNameRef.setValue(name);
// to add email
String email = etEmail.getText().toString().trim();
DatabaseReference userEmail = myRootRef.child("Email ");
userEmail.setValue(email);
以同样的方式,你可以做其余的事情
这会将数据存储为JSON树,其中根为myRootRef
,其子项为userNameRef
答案 2 :(得分:1)