0

I am creating an application that allows a user to sign in and register. Currently I am using firebase to store this data but I require more data than just email and password. I am able to store multiple fields to a real time database however my log in page is using the Authentication with firebase. Is there anyway of connecting the two?

Log in page

 //creating a new user
        firebaseAuth.signInWithEmailAndPassword(username, password)
                .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(getApplicationContext(), FAQ.class));
                            finish();
                        } else{
                            Toast.makeText(MainActivity.this, "Login Error", Toast.LENGTH_LONG).show();
                        }
                    }
                });

    }

Registration page

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

        //firebaseAuth =FirebaseAuth.getInstance();
        userN=(EditText)findViewById(R.id.userN);
        password2=(EditText)findViewById(R.id.password2);

        gender2=(EditText)findViewById(R.id.gender2);
        headInjury2=(EditText)findViewById(R.id.headInjury2);
        Smoker2=(EditText)findViewById(R.id.Smoker2);
        dateOfBirth2=(EditText)findViewById(R.id.dateOfBirth2);
        submitBtn2=(Button)findViewById(R.id.submitBtn2);
        user= new User();
        reff= FirebaseDatabase.getInstance().getReference().child("User");

        submitBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int date=Integer.parseInt(dateOfBirth2.getText().toString().trim());


                user.setUserN(userN.getText().toString().trim());
                user.setPassword2(password2.getText().toString().trim());
                user.setGender2(gender2.getText().toString().trim());
                user.setHeadInjury2(headInjury2.getText().toString().trim());
                user.setSmoker2(Smoker2.getText().toString().trim());
                user.setDOB(date);
                reff.push().setValue(user);
                Toast.makeText(Register.this, "Successfully registered", Toast.LENGTH_LONG).show();


            }
        });
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

To store additional data about a user in the Realtime Database, you'll typically want to store that information under the user's UID.

So you'd have something like

Users: {
  uidOfCaitlin: {
    firstName: "Caitlin",
    lastName: "Daly McDowell",
    dob: "2020-03-10"
  },
  uidOfFrank: {
    firstName: "Frank",
    lastName: "van Puffelen",
    dob: "1970-03-10"
  }
}

To store the data like this, you'd do something like:

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

usersRef.child(uid).setValue(user);

This topic has been covered quite a bit before, so I also recommend you check out:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks! :) is there anyway of using the real time database to log a user into the application? – Caitlin Daly McDowell Mar 10 '20 at 17:16
  • @CaitlinDalyMcDowell Signing in is something you do with the Firebase Authentication API. Once a user is signed in there, you can use their UID in your client-side code **and** in the data structure, and you can securely use their UID in the server-side security rules for the database. See the links that I provided in my answer, as they give more examples. – Frank van Puffelen Mar 10 '20 at 18:15