android – 如何在用户注册期间在firebase中的实时数据库中存储数据?

我正在制作具有用户个人资料的类似社交媒体的应用.我想在使用他们的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);
    } 
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...