Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1 You have to design 2 screens. 
a) Registration screen 
b) Dashboard screen 

2) Registration screen should contain below fields: 
a) First Name 
b) Last Name 
c) Email 
d) Mobile Number 
e) Password 
f) Confirm Password

3) Registration Form should have standard validation like:

a) User can enter max 50 characters for First Name 
b) User can enter max 100 characters for Last Name 
c) Email standard validation (xx@xx.xx) 
d) Password should be in hidden 
e) Users can enter max 20 characters for Passwords. 

4) Registration screen records should be inserted into the Firebase Realtime Database. 

5) After submitting a record on the Registration screen, the user should redirect to the Dashboard screen. 

6) On Dashboard Screen, Get all the users data from database and display list of users.

I have done the authentication is working fine but not showing in realtime database.


What I have tried:

Package com.example.registration;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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.crashlytics.buildtools.reloc.org.apache.http.util.TextUtils;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

            private EditText  edfname,edlname,edemail,edpassword,edcpassword,edphone;
            private Button btregister;

            FirebaseAuth fAuth;

        // creating a variable for our
        // Firebase Database.
        FirebaseDatabase firebaseDatabase;

        // creating a variable for our Database
        // Reference for Firebase.
        DatabaseReference databaseReference;

        // creating a variable for
        // our object class

        RegisterInfo registerInfo;

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

        // initializing our edittext and button
        edfname = findViewById(R.id.edfname);
        edlname = findViewById(R.id.edlname);
        edemail = findViewById(R.id.edemail);
        edpassword = findViewById(R.id.edpassword);
        edcpassword = findViewById(R.id.edcpassword);
        edphone = findViewById(R.id.edphone);

        // below line is used to get the
        // instance of our FIrebase database.
        firebaseDatabase = FirebaseDatabase.getInstance();

        // below line is used to get reference for our database.
        databaseReference = firebaseDatabase.getReference("RegisterInfo");

        // initializing our object
        // class variable.
        registerInfo = new RegisterInfo();

        btregister = findViewById(R.id.btregister);

        // adding on click listener for our button.
        btregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

        // getting text from our edittext fields.
        String fname = edfname.getText().toString().trim();
        String lname = edlname.getText().toString().trim();
        String email= edemail.getText().toString().trim();
        String password = edpassword.getText().toString().trim();
        String cpassword = edcpassword.getText().toString().trim();
        String phone = edphone.getText().toString().trim();




        fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                        if(task.isSuccessful()){
                                Toast.makeText(MainActivity.this, "User created", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(getApplicationContext(),Dashboard.class));
                        }
                        else{
                                Toast.makeText(MainActivity.this, "Error !" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();

                        }
                }
        });

        // below line is for checking weather the
        // edittext fields are empty or not.
        if (TextUtils.isEmpty(fname) && TextUtils.isEmpty(lname) && TextUtils.isEmpty(email) && TextUtils.isEmpty(password) && TextUtils.isEmpty(cpassword)&& TextUtils.isEmpty(phone) ) {
        // if the text fields are empty
        // then show the below message.
        Toast.makeText(MainActivity.this, "Please add some data.", Toast.LENGTH_SHORT).show();
        } else {
        // else call the method to add
        // data to our database.
        addDatatoFirebase(fname,lname,email,password,cpassword,phone);
        }
        }
        });
        }

private void addDatatoFirebase(String fname, String lname, String email,String password, String cpassword, String phone) {
        // below 3 lines of code is used to set
        // data in our object class.
        registerInfo.setRfname(fname);
        registerInfo.setRlanme(lname);
        registerInfo.setRemail(email);
        registerInfo.setRpassword(password);
        registerInfo.setRcpassword(cpassword);
        registerInfo.setRphone(phone);

        // we are use add value event listener method
        // which is called with database reference.
        databaseReference.addValueEventListener(new ValueEventListener() {
@Override



public void onDataChange(@NonNull DataSnapshot snapshot) {
        // inside the method of on Data change we are setting
        // our object class to our database reference.
        // data base reference will sends data to firebase.
        databaseReference.setValue(registerInfo);

        // after adding this data we are showing toast message.
        Toast.makeText(MainActivity.this, "data added", Toast.LENGTH_SHORT).show();
        }

@Override
public void onCancelled(@NonNull DatabaseError error) {
        // if the data is not added or it is cancelled then
        // we are displaying a failure toast message.
        Toast.makeText(MainActivity.this, "Fail to add data " + error, Toast.LENGTH_SHORT).show();
        }
        });
        }
        }
Posted
Comments
Richard Deeming 10-Jan-22 4:49am    
You seem to have forgotten to ask a question.

However, you do appear to be storing passwords in plain text, which is a major security problem:
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

And you seem to be storing both the "password" and "confirm password" values, which is pointless - your code should be validating that those two values are identical, so there's no point storing it twice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900