繁体   English   中英

如何使用 Firebase 发送验证电子邮件?

[英]How to send verification email with Firebase?

我正在使用 Firebase 的电子邮件和密码方法注册我的用户。 像这样:

mAuth.createUserWithEmailAndPassword(email, password)

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

    if (task.isSuccessful()) {

        FirebaseUser signed = task.getResult().getUser();

        writeNewUser(signed.getUid());

        new android.os.Handler().postDelayed(

                new Runnable() {
                    public void run() {

                        updateUser(b);

                    }
                }, 3000);

    } else {

        new android.os.Handler().postDelayed(

                new Runnable() {
                    public void run() {

                        onSignupFailed();

                    }
                }, 3000);

    }

    }
});

成功注册用户的电子邮件后,我希望 Firebase 发送验证电子邮件。 我知道使用 Firebase 的sendEmailVerification可以做到这一点。 除了发送此电子邮件之外,我还希望在用户验证电子邮件之前禁用该用户的帐户。 这还需要使用 Firebase 的isEmailVerified功能。 但是,我没有成功让 Firebase 发送验证电子邮件,我无法弄清楚如何禁用和启用发送验证电子邮件的帐户,并在验证后启用它。

这个问题是关于如何使用 Firebase 发送验证邮件。 OP 无法弄清楚如何禁用和启用发送验证电子邮件的帐户以及在验证后如何禁用和启用帐户。

此外,这在 firebase 文档中没有正确记录。 因此,我正在编写一个分步程序,如果有人遇到问题,他/她可以遵循该程序。

1) 用户可以使用 createUserWithEmailAndPassword 方法。

示例:

mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("TAG", "createUserWithEmail:onComplete:" + task.isSuccessful());

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            // Show the message task.getException()
                        }
                        else
                        {
                            // successfully account created
                            // now the AuthStateListener runs the onAuthStateChanged callback
                        }

                        // ...
                    }
                });

如果创建了新帐户,用户也会登录,并且 AuthStateListener 运行 onAuthStateChanged 回调。 在回调中,您可以管理向用户发送验证邮件的工作。

示例:

onCreate(...//
mAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            // User is signed in
            // NOTE: this Activity should get onpen only when the user is not signed in, otherwise
            // the user will receive another verification email.
            sendVerificationEmail();
        } else {
            // User is signed out

        }
        // ...
    }
};

现在发送验证邮件可以写成:

private void sendVerificationEmail()
    {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        user.sendEmailVerification()
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            // email sent


                                    // after email is sent just logout the user and finish this activity
                                    FirebaseAuth.getInstance().signOut();
                                    startActivity(new Intent(SignupActivity.this, LoginActivity.class));
                                    finish();
                        }
                        else
                        {
                            // email not sent, so display message and restart the activity or do whatever you wish to do

                                    //restart this activity
                                    overridePendingTransition(0, 0);
                                    finish();
                                    overridePendingTransition(0, 0);
                                    startActivity(getIntent());

                        }
                    }
                });
    }

现在来到登录活动:

在这里,如果用户成功登录,那么我们可以简单地调用一个方法,您可以在其中编写用于检查电子邮件是否已验证的逻辑。

示例:

mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        //Log.d("TAG", "signInWithEmail:onComplete:" + task.isSuccessful());

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            //Log.w("TAG", "signInWithEmail:failed", task.getException());

                        } else {
                            checkIfEmailVerified();
                        }
                        // ...
                    }
                });

现在考虑 checkIfEmailVerified 方法:

private void checkIfEmailVerified()
{
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    if (user.isEmailVerified())
    {
        // user is verified, so you can finish this activity or send user to activity which you want.
        finish();
        Toast.makeText(LoginActivity.this, "Successfully logged in", Toast.LENGTH_SHORT).show();
    }
    else
    {
        // email is not verified, so just prompt the message to the user and restart this activity.
        // NOTE: don't forget to log out the user.
        FirebaseAuth.getInstance().signOut();

        //restart this activity

    }
}

所以我在这里检查电子邮件是否经过验证。 如果没有,则注销用户。

所以这是我正确跟踪事情的方法。

将验证发送到用户的电子邮件

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

检查用户是否通过验证

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
boolean emailVerified = user.isEmailVerified();

使用FirebaseAuth.getInstance().getCurrentUser().sendEmailVerification()FirebaseAuth.getInstance().getCurrentUser().isEmailVerified()

无法通过 Firebase SDK 禁用该帐户。 您可以做的是使用包含 Firebase 身份验证 ID 令牌的GetTokenResult并针对您的自定义后端对其进行验证,或者为与该用户对应的 Firebase 数据库设置一个标志。 我个人会使用 Firebase 数据库中的标志

要首先使用 Firebase 发送电子邮件链接,您需要使用我们在 Firebase 上通过以下方式创建用户的实例来获取 FirebaseAuth 实例:

firebaseauth.createUserWithEmailAndPassword(email,pass);

当方法返回成功时,我们使用 Firebase 用户实例向用户发送验证链接,如下所示:

 final FirebaseUser user = mAuth.getCurrentUser();
                      user.sendEmailVerification()

请参阅此链接: https : //technicalguidee.000webhostapp.com/2018/10/email-verification-through-link-using-firebase-authentication-product-android

   mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful()){

                    mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {

                            if (task.isSuccessful()) {



                                Toast.makeText(this, "please check email for verification.", Toast.LENGTH_SHORT).show();
                                loadingBar.dismiss();
                            }else{
                                Toast.makeText(this, task.getException().getMessage() , Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

对于科特林

val user: FirebaseUser? = firebaseAuth.currentUser
user?.sendEmailVerification()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM