Firebase电子邮件身份验证向firebase数据库添加用户名和详细信息

时间:2017-08-17 06:11:39

标签: android firebase firebase-realtime-database firebase-authentication

目前,我使用firebase电子邮件身份验证创建新用户是成功的,但我想在firebase数据库中添加用户名,地址等个人详细信息。我做了所有的研究,使用uid从电子邮件身份验证作为孩子到节点,但代码仍然不工作插入任何东西到数据库甚至android studio显示数据库连接。那么有人可以就如何解决这个问题向我提出建议吗?

package com.example.rex.ota20;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import java.util.Map;

public class Register extends AppCompatActivity {

private FirebaseAuth auth;
private EditText run, rpw,re;
//private EditText rcfpw, rage, , rpn, ra, rpc;
//private RadioButton rbm, rbfm, rbstd, rbtt;
private String usnm, pswd, email, uid;
private FirebaseDatabase db;
private DatabaseReference ref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    auth = FirebaseAuth.getInstance();
    db = FirebaseDatabase.getInstance();
    ref = db.getReference();
    FirebaseUser user = auth.getCurrentUser();
    uid = user.getUid();//getuser id

    run = (EditText) findViewById(R.id.Run);
    rpw = (EditText) findViewById(R.id.Rpw);
    //rcfpw = (EditText) findViewById(R.id.Rcpw);
    //rage = (EditText) findViewById(R.id.Rage);

    //rbm = (RadioButton) findViewById(R.id.rbmale);
    //rbfm = (RadioButton) findViewById(R.id.rbfemale);

    re = (EditText) findViewById(R.id.Re);
    //rpn = (EditText) findViewById(R.id.Rpn);
    //ra = (EditText) findViewById(R.id.Ra);
    //rpc = (EditText) findViewById(R.id.Rpc);

    //rbstd = (RadioButton) findViewById(R.id.rbstd);
    //rbtt= (RadioButton) findViewById(R.id.rbtt);

    final Button br = (Button) findViewById(R.id.Rbt);



    br.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //if(!TextUtils.isEmpty(usnm) || !TextUtils.isEmpty(pswd) || !TextUtils.isEmpty(email)){
            usnm = run.getText().toString().trim();
            pswd = rpw.getText().toString().trim();
            //confirmpw = rcfpw.getText().toString().trim();
            email = re.getText().toString().trim();
            register(usnm, pswd, email);

            //}// end of validate
        }//end of onclick
    });
}

private void register (final String un, final String pw, final String em){
    Task<AuthResult> authResultTask = auth.createUserWithEmailAndPassword(em, pw).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {
                DatabaseReference dbref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://otapp-61bef.firebaseio.com/").child("users");
                uid  = dbref.push().getKey();
                Toast.makeText(Register.this, "Success Registed", Toast.LENGTH_LONG).show();
                Intent i = new Intent(Register.this, StudentHomePage.class);
                startActivity(i);
                finish();
            } else {
                Toast.makeText(Register.this, "Error", Toast.LENGTH_LONG).show();
            }
        }
    });
}// end of signup

/*private void generateUser (String username, String password, String email){
    User u = new User (username, password, email);
    dbref.child("users").setValue(u);
}*/
}

并且规则是

{
 "rules": {
  ".read": true,
  ".write": true
  }
}

1 个答案:

答案 0 :(得分:1)

工作示例:

用户类

@IgnoreExtraProperties
public class User {

    private String name;
    private String surname;
    private String phone;

    public User() {

    }

    public User(String phone) {
        this.phone = phone;
    }

    public User(String name, String surname, String phone) {
        this.name = name;
        this.surname = surname;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getPhone() {
        return phone;
    }
}

活动方法

private void writeNewUser(DatabaseReference databaseReference, String id, String name, String surname, String phone) {
        User user = new User(name, surname, phone);
        databaseReference.child(id).setValue(user);
    }