Create new user with Names , Username etc in fireb

2020-08-01 05:39发布

问题:

I'm new android learner so its is difficult for me to do stuffs which I cannot find in the documentation. Here is my code for creating users

 mAuth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        //Successfully Registered
                        Toast.makeText(RegisterActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                    }else {
                        //Error occurred during registration
                        Toast.makeText(RegisterActivity.this, "Registration Unsuccessful", Toast.LENGTH_SHORT).show();
                        try {
                            throw task.getException();
                        } catch(FirebaseAuthWeakPasswordException e) {
                            editTextPassword.setError(e.getMessage());
                            editTextPassword.requestFocus();
                        }catch(FirebaseAuthUserCollisionException | FirebaseAuthInvalidCredentialsException e) {
                            editTextEmail.setError(e.getMessage());
                            editTextEmail.requestFocus();
                        } catch(Exception e) {
                            Log.e(RegisterActivity.class.getName(), e.getMessage());
                        }
                    }
                    progressDialog.dismiss();
                }
            });

This only takes two parameters(email and password) to create an user. To create user with more fields what approach should I take.

I have also added a FirebaseAuth.AuthStateListener() to check user login status. But when I'm calling firebaseAuth.getCurrentUser().getDisplayName() after successfully user login it returns null as usual.So how can I create user with Names so I can retrieve it with firebaseAuth.getCurrentUser().getDisplayName().

回答1:

After the registration is successful,

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

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Jane Q. User")
        .build();

EDIT: This code is a bit incomplete as profileUpdates is never accessed.

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

Then, to retrieve it, use this wherever required,

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email address etc
    String name = user.getDisplayName();
    String email = user.getEmail();
}


回答2:

To add a user with extra information such as User's name or other required information you should store these data using the Firebase real-time database under the unique user_id generated upon successful completion of email and password registration.

Get user input for name in registration form,

String name = mNameField.getText().toString().trim();

Add user's name in onComplete method :

 @Override
 public void onComplete(@NonNull Task<AuthResult> task) {
 if(task.isSuccessful()){

     String user_id = mAuth.getCurrentUser().getUid;

     DatabaseReference current_user = mDatabase.child(user_id);

     current_user.child("name").setValue(name);

     progressDialog.dismiss();

  }