-1

I have recently adopted Firebase for my Backend work. Using Firebase I want to store all the user data like username, phone number, etc to Firebase in the same RegisterActivity and not just Email & Password. How can I achieve this ?

My RegisterActivity will only appear at the time of installation. When user have register to my app, I am destroying the activity. So, there is no instance of RegisterActivity further.

RegisterActivity - onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.activity_register);

    initialization();
    underlineText();    //Underlining Text in App

    userObj = new User();
    userObj.setName(NAME);
    userObj.setEMAIL(EMAIL);
    userObj.setPHONE(PHN);

    animShake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);     //Animation

    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);    //Vibration

    reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            submitForm();   //Registration Click Listener
        }
    });

    skip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            skipRegistrationSection();  //Skip Click Listener
        }
    });

    runAtInstallation();
}

submitform():

private void submitForm() {

    if (!checkName()) {
        name.setAnimation(animShake);
        name.startAnimation(animShake);
        vib.vibrate(60);
        return;
    }
    if (!checkEmail()) {
        email.setAnimation(animShake);
        email.startAnimation(animShake);
        vib.vibrate(60);
        return;
    }
    if (!checkPhone()) {
        phone.setAnimation(animShake);
        phone.startAnimation(animShake);
        vib.vibrate(60);
        return;
    }
    if (!checkPassword()) {
        password.setAnimation(animShake);
        password.startAnimation(animShake);
        vib.vibrate(60);
        return;
    }
    if (!checkConfirmPassword()) {
        confirmPassword.setAnimation(animShake);
        confirmPassword.startAnimation(animShake);
        vib.vibrate(60);
        return;
    }

    nameLayout.setErrorEnabled(false);
    emailLayout.setErrorEnabled(false);
    phoneLayout.setErrorEnabled(false);
    passwordLayout.setErrorEnabled(false);
    confirmPasswordLayout.setErrorEnabled(false);

    NAME = name.getText().toString().trim();
    EMAIL = email.getText().toString().trim();
    PHN = phone.getText().toString().trim();
    PASSWORD = password.getText().toString();

    progressBar.setVisibility(View.VISIBLE);

    authUser();     //authenticating  User via Email & Password

}

authUser():

private void authUser() {

    mFirebaseAuth.createUserWithEmailAndPassword(EMAIL, PASSWORD).addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if (!task.isSuccessful()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                builder.setMessage(task.getException().getMessage())
                        .setTitle("Error")
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
                progressBar.setVisibility(View.GONE);
            } else {
                progressBar.setVisibility(View.GONE);

                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        }

    });
}

runAtInstallation:

private void runAtInstallation() {

    SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
    if (pref.getBoolean("activity_executed", false)) {

        Intent act = new Intent(getApplicationContext(), MainActivity.class);
        act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        act.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(act);
        finish();
    } else {
        SharedPreferences.Editor ed = pref.edit();
        ed.putBoolean("activity_executed", true);
        ed.commit();
    }
}

I want to save name,email,& phone in the firebase database during registration and to destroy the activity after that.

AL.
  • 36,815
  • 10
  • 142
  • 281
Mohit Khaitan
  • 113
  • 11

3 Answers3

1

I'm working on the same problem right now. The thing is, sign-IN (authentication) and sign-UP (registration) are two different things.

What I have done is have two separate activities, signIN... and signUP (register).

Once the user is signed up (email and password), they will have a unique userID known to Firebase.

Next, they go to the registration activity, so when you "upload" all the data from the editTexts in this activity, you can upload them to a node (key... index) in your database that matches the userID... so your data in your database looks like:

\ mydatabase \ users \ [uniqueID] \

If you combine both activies (authentication and registration) into one... with many fields, "email, password, name, phonenumber, etc." all in one activity, then you're going to still need to make a separate signIN only activity for the next time they run the app with an expired session. I think it's much simpler to do two separate activites.

Community
  • 1
  • 1
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • I have two separate activities for sign-in & sign-up. But in the sign-up i want to save the username,email,& phone in the database and the direct the user to the `MainActivity`. – Mohit Khaitan Sep 16 '16 at 11:36
  • In your question there are 3 things, sign-in, sign-up, register. so without sign-up, user cannot sign-in and register is completely a different thing. But more or less we both have the same problem. – Mohit Khaitan Sep 16 '16 at 11:43
  • I think if @XaverKapeller takes a look he will have a much better answer than I can give you... even though I do describe some possible solutions in my post. – Nerdy Bunz Sep 17 '16 at 06:28
0

I have one activity and I simply try to register right away regardless if the user exists or not and then if the error is due to the email existing already I sign the user in instead of registering them.

mAuth = FirebaseAuth.getInstance();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {                       
if (!task.isSuccessful()) {
if(task.getException() instanceof FirebaseAuthUserCollisionException) {
// login user
}
else {
// handle error
}
}    
else {
// register user
}
}
Riley MacDonald
  • 453
  • 1
  • 4
  • 15
  • But then they would have filled out all those registration forms for no reason, no? And they would also be confused and think their account has been deleted. – Nerdy Bunz Sep 16 '16 at 23:06
  • For me it is a simple email and password login, therefore they do not need to fill out a huge registration form again when logging in. – Riley MacDonald Sep 17 '16 at 10:42
0

You can add user profile information and other details like

For update profile

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Jane Q. User")
        .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
        .build();

user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User profile updated.");
                }
            }
        });

For more details to manage user details please refer this link Firebase manage user

Upendra Shah
  • 2,218
  • 17
  • 27