Change DisplayName in Firebase

前端 未结 1 1429
北恋
北恋 2020-12-22 02:18

I am using email registration Firebase. As of now their is no option to set DisplayName in Firebase. So I am redirecting users to a class where they can change DisplayName s

相关标签:
1条回答
  • 2020-12-22 03:06

    You have to follow this option

    @Override
    protected void onStart() {
        super.onStart();
        firebaseAuth.addAuthStateListener(mAuthListener); //firebaseAuth is of class FirebaseAuth
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            firebaseAuth.removeAuthStateListener(mAuthListener);
        }
    }
    

    and in your OnCreate() or onCreateView() implment this:

    firebaseAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    if (!LoginFragment.displayName.isEmpty()) {
                        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                .setDisplayName(LoginFragment.displayName).build();
                        user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Log.d("Display name: ", FirebaseAuth.getInstance().getCurrentUser().getDisplayName());
                                }
                            }
                        });
                    }
                    LoginFragment.displayName = "";
                }
            }
        };
    
    0 讨论(0)
提交回复
热议问题