In this tutorial, we are going to store the user’s data we get in signup activities inside the firebase realtime database. In the previous activity, we performed phone number verification using firebase and now we will store data once the phone number has been verified!
Store Data in Firebase Pre-requisites
The first thing that we need to store the data in firebase is to connect our android studio app with firebase and here is how to set up firebase?
After that we have to make sure, our firebase is connected and all the dependencies are set up. We will open the firebase console and select the realtime database. Then set rules to as it as for now. You can get everything here.
Get data from previous activities
As we call the OTP screen from previous screens so, we have to pass all the data or phone number from called activities like signup or forget the password to perform the phone authentication. So this is the code used to pass data from previous activities using Intent.
At the end line, you can see a new string with name whatToDO is basically a check to identify which activity is calling the OTP screen as we will use the same OTP screen to verify the user’s phone number if her/she forgets the password. What we passed from previous activity is here.
Get Data from previous activities
//Get all the data from Intent
fullName = getIntent().getStringExtra("fullName");
email = getIntent().getStringExtra("email");
username = getIntent().getStringExtra("username");
password = getIntent().getStringExtra("password");
date = getIntent().getStringExtra("date");
gender = getIntent().getStringExtra("gender");
phoneNo = getIntent().getStringExtra("phoneNo");
whatToDO = getIntent().getStringExtra("whatToDO");
Store Data function
Once the verification completed and we reached in to the if(task.isSuccessful()) then we will store the data to firebase! So for that let’s just call a new function in here that will store the data!
We will add a check here as I talk about above to check weather we have to create a new account or verify the old user’s phone number.
When Authentication Succeeded
if (task.isSuccessful()) {
//Verification completed successfully here Either
// store the data or verify the old user
if (whatToDO.equals("updateData")) {
updateOldUsersData();
} else if (whatToDO.equals("createNewUser")) {
storeNewUsersData();
}
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(VerifyOTP.this, "Verification Not Completed! Try again.", Toast.LENGTH_SHORT).show();
}
}
Create Helper Class
This is the class we used to store all the new data record of user! All we have to do is to call this class and pass data in it then pass this class reference to the database!
User Helper Class
package com.taimoorsikander.cityguide.Databases;
public class UserHelperClass {
String fullName, username, email, phoneNo, password, date, gender;
public UserHelperClass() {
}
public UserHelperClass(String fullName, String username, String email, String phoneNo, String password, String date, String gender) {
this.fullName = fullName;
this.username = username;
this.email = email;
this.phoneNo = phoneNo;
this.password = password;
this.date = date;
this.gender = gender;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Perform Firebase queries
This is the function which will be called when there is a new user what to create account.
Store New User Function
private void storeNewUsersData() {
FirebaseDatabase rootNode = FirebaseDatabase.getInstance();
DatabaseReference reference = rootNode.getReference("Users");
//Create helperclass reference and store data using firebase
UserHelperClass addNewUser = new UserHelperClass(fullName, username, email, phoneNo, password, date, gender);
reference.child(phoneNo).setValue(addNewUser);
//We will also create a Session here in next videos to keep the user logged In
startActivity(new Intent(getApplicationContext(), RetailerDashboard.class));
finish();
}