如何在Android中更新来自Firebase的电子邮件?

时间:2018-03-19 06:42:38

标签: java android firebase firebase-authentication

我正在使用此代码:

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

user.updateEmail("user@example.com")
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User email address updated.");
                }
            }
        });

但我仍然无法更新登录用户的用户电子邮件ID。其他工作正常但不是这个。

3 个答案:

答案 0 :(得分:9)

您需要重新验证您的用户身份。根据文档,更改主电子邮件地址是一项敏感操作。

重新认证:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        // Get auth credentials from the user for re-authentication
        AuthCredential credential = EmailAuthProvider
                .getCredential("user@example.com", "password1234"); // Current Login Credentials \\
        // Prompt the user to re-provide their sign-in credentials
        user.reauthenticate(credential)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Log.d(TAG, "User re-authenticated.");
                        //Now change your email address \\
                        //----------------Code for Changing Email Address----------\\
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        user.updateEmail("user@example.com")
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {
                                            Log.d(TAG, "User email address updated.");
                                        }
                                    }
                                });
                        //----------------------------------------------------------\\
                    }
                });

答案 1 :(得分:0)

对于Kotlin

 // need to sign user in immediately before updating the email 
        auth.signInWithEmailAndPassword("currentEmail","currentPassword")
        .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success now update email                
                    auth.currentUser!!.updateEmail(newEmail)
                        .addOnCompleteListener{ task ->
                        if (task.isSuccessful) {
               // email update completed
           }else{
               // email update failed
                    }
       }
       } else {
                    // sign in failed
                }
            }

答案 2 :(得分:-1)

您在firebase数据库中使用了以下代码来更新记录..

    /**
 * User data change listener
 */
private void updateRecord(String Email) {
    // User data change listener
    String userId = mFirebaseDatabase.push().getKey();
    mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            user.setEmail(Email);
            mFirebaseDatabase.child(userId).setValue(user);

        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.e(TAG, "Failed to read user", error.toException());
        }
    });
}