📜  如何在Android中从Firebase更新/更改电子邮件?

📅  最后修改于: 2021-05-10 14:21:51             🧑  作者: Mango

在任何应用程序中,我们都具有使用我们的电子邮件和密码登录的功能。有时会发生我们想要更改电子邮件或丢失了先前电子邮件的密码的情况,因此在这里我们将实现相同的功能,以使用Firebase身份验证更改电子邮件。注意,我们将使用Java语言实现该项目

分步实施

让我们考虑您已经存在一个项目,或者您可以创建一个新项目。

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用XML文件

转到XML文件并参考以下代码。下面是XML文件的代码。

XML


    
    
  
    
  
    
  
    


Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
// Get auth credentials from the user for re-authentication
AuthCredential credential = EmailAuthProvider.getCredential(email, password); // Current Login Credentials
  
// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        
                        Log.d("value", "User re-authenticated.");
  
                        // Now change your email address \\
                        //----------------Code for Changing Email Address----------\\
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        user.updateEmail(change.getText().toString()).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(ChangeEmail.this, "Email Changed" + " Current Email is " + change.getText().toString(), Toast.LENGTH_LONG).show();
                                        }
                             }
                 });
       }
});


Java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
  
public class ChangeEmail extends AppCompatActivity {
  
    FirebaseAuth auth;
    Button chanegemail;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_email);
  
        // Initialising the email and password
        final EditText email = findViewById(R.id.email);
        final EditText password = findViewById(R.id.logpass);
        chanegemail = findViewById(R.id.changeemail);
  
        // click on this to change email
        chanegemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeemail(email.getText().toString(), password.getText().toString());
            }
        });
  
    }
  
    EditText change;
  
    // Here we are going to change our email using firebase re-authentication
    private void changeemail(String email, final String password) {
  
        change = findViewById(R.id.chaneg);
  
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
        // Get auth credentials from the user for re-authentication
        AuthCredential credential = EmailAuthProvider.getCredential(email, password); // Current Login Credentials
  
        // Prompt the user to re-provide their sign-in credentials
        user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
  
                        Log.d("value", "User re-authenticated.");
  
                        // Now change your email address \\
                        //----------------Code for Changing Email Address----------\\
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        user.updateEmail(change.getText().toString()).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(ChangeEmail.this, "Email Changed" + " Current Email is " + change.getText().toString(), Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                         }
             });
    }
}


步骤3:使用Java文件

因此,主要部分存在于Java文件中。在此文件中,我们需要根据文档进行重新认证,因为更改主电子邮件地址是一项敏感操作。下面是执行此操作的代码段。

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
// Get auth credentials from the user for re-authentication
AuthCredential credential = EmailAuthProvider.getCredential(email, password); // Current Login Credentials
  
// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        
                        Log.d("value", "User re-authenticated.");
  
                        // Now change your email address \\
                        //----------------Code for Changing Email Address----------\\
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        user.updateEmail(change.getText().toString()).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(ChangeEmail.this, "Email Changed" + " Current Email is " + change.getText().toString(), Toast.LENGTH_LONG).show();
                                        }
                             }
                 });
       }
});

以下是的完整代码 上述XML文件Java文件

Java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
  
public class ChangeEmail extends AppCompatActivity {
  
    FirebaseAuth auth;
    Button chanegemail;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_email);
  
        // Initialising the email and password
        final EditText email = findViewById(R.id.email);
        final EditText password = findViewById(R.id.logpass);
        chanegemail = findViewById(R.id.changeemail);
  
        // click on this to change email
        chanegemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeemail(email.getText().toString(), password.getText().toString());
            }
        });
  
    }
  
    EditText change;
  
    // Here we are going to change our email using firebase re-authentication
    private void changeemail(String email, final String password) {
  
        change = findViewById(R.id.chaneg);
  
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
        // Get auth credentials from the user for re-authentication
        AuthCredential credential = EmailAuthProvider.getCredential(email, password); // Current Login Credentials
  
        // Prompt the user to re-provide their sign-in credentials
        user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
  
                        Log.d("value", "User re-authenticated.");
  
                        // Now change your email address \\
                        //----------------Code for Changing Email Address----------\\
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        user.updateEmail(change.getText().toString()).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(ChangeEmail.this, "Email Changed" + " Current Email is " + change.getText().toString(), Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                         }
             });
    }
}

输出: