1

I want to upload some user info via firebase libraries in Android Studio. The problem I have is that I am not sure how to glue together the functions so that it works.

So far I have the email validation function and also the upload user data function, but when I hit the register button, the email verification is sent and it works(the user is registered, but the user data is not uploaded to the firebase data base.

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.EditText;
import android.widget.Button;
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;

public class RegisterActivity extends AppCompatActivity {

    private EditText userName, userPassword, userEmail, phoneNumber;
    private Button logButton, regButton;
    private FirebaseAuth firebaseAuth;
    String email, name, phone, password;

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

        firebaseAuth = FirebaseAuth.getInstance();

        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(validate()){
                    //upload everithing to the database
                    String user_name = userName.getText().toString().trim();
                    String user_email = userEmail.getText().toString().trim();
                    String user_password = userPassword.getText().toString().trim();
                    String user_phone = phoneNumber.getText().toString().trim();

                    firebaseAuth.createUserWithEmailAndPassword(userEmail.getText().toString().trim(), userPassword.getText().toString().trim()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful()) {
                               sendEmailVerification();
                            }
                            else{
                                Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
            }
        });
        logButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
            }
        });
    }

    private void setupUIViews(){
        userName = (EditText)findViewById(R.id.etUser2);
        userPassword = (EditText)findViewById(R.id.etPas2);
        userEmail = (EditText)findViewById(R.id.etEmail2);
        phoneNumber = (EditText)findViewById(R.id.etPhone1);
        regButton = (Button)findViewById(R.id.registerButton2);
        logButton = (Button)findViewById(R.id.loginButton2);

    }

    private Boolean validate(){
        Boolean result = false;

         name = userName.getText().toString().trim();
         email = userEmail.getText().toString().trim();
         phone = phoneNumber.getText().toString().trim();
         password = userPassword.getText().toString().trim();

        if(name.isEmpty() || password.isEmpty() || email.isEmpty()){
           Toast.makeText(this, "Please enter all the details", Toast.LENGTH_SHORT).show();
        }
        else{
            result = true;
        }
        return result;
    }

    private void sendEmailVerification(){
        final FirebaseUser firebaseUser = firebaseAuth.getInstance().getCurrentUser();

        if(firebaseUser != null){
            firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        sendUserData();
                        Toast.makeText(RegisterActivity.this, "Sucessfully registered", Toast.LENGTH_SHORT).show();
                        firebaseAuth.signOut();
                        finish();
                        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                    }
                    else{
                        Toast.makeText(RegisterActivity.this, "Verification email was not sent", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void sendUserData(){
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
        UserProfile userProfile = new UserProfile(name, email, phone);
        myRef.child(firebaseAuth.getUid()).setValue(userProfile);
    }
}

and also the UserProfile class:

package com.example.myapplication;

public class UserProfile {
    public String userName;
    public String userEmail;
    public String userPhone;

    public UserProfile(String userName, String userEmail, String userPhone) {
        this.userName = userName;
        this.userEmail = userEmail;
        this.userPhone = userPhone;
    }
}

So in conclusion I want to have the user data into the database while also having the email validation. Thanks in advance!

5
  • I tried your code and it works properly. Commented May 29, 2019 at 18:08
  • I don't seem to get any of the registered user data into the database. I have now idea why Commented May 29, 2019 at 18:12
  • Try to debug the code and check wether the sendUserData() is called and working Commented May 29, 2019 at 18:22
  • Looks like somehow firebaseAuth.signOut(); interferes with the user data sending to the data base. I've tried removing it and now it works. But now I'm in the case where the user is automated logged in without validating his email... It's like I'm chasing my tail around Commented May 29, 2019 at 18:28
  • doing that, somehow fails the registration... Commented May 29, 2019 at 18:33

1 Answer 1

1

Add a listener for the setValue() method and call signOut() when the data is written successfully.

private void sendUserData(){
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
    UserProfile userProfile = new UserProfile(name, email, phone);
    myRef.child(firebaseAuth.getUid()).setValue(userProfile).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            firebaseAuth.signOut();
        }
    });
}

Hope this will fix the issue. BTW your code seems to have many duplications such as reading the inputs from the textviews many times.

Sign up to request clarification or add additional context in comments.

1 Comment

It kind of works. But, for some strange reason, it sends me to the user profile activity(that I made), as if the user automatically logged in. But the strange part is that I didn't put any redirection to the user profile activity in the actual code. firebaseAuth().signout(); only worked in the sendEmailVerification() somehow, there it returned to the MainActivity as I intended. Also I did some cleaning in the code and got read of the duplicates :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.