我正在制作具有用户个人资料的类似社交媒体的应用.我想在使用他们的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(); } } }); } }
解决方法
从
createUserWithEmailAndPassword method reference
Tries to create a new user account with the given email address and password. If successful,it also signs the user in into the app.
要确保任务完成且firebaseUser不为null,最好将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); } }