如何使用 Firebase 发送验证电子邮件? [英] How to send verification email with Firebase?

查看:31
本文介绍了如何使用 Firebase 发送验证电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am signing up my users using Firebase's email and password method. like this:

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 发送验证电子邮件,我无法弄清楚如何禁用和启用发送验证电子邮件的帐户,并在验证后启用.

After the user's email has been successfully registered, I would like Firebase to send a verification email. I know this is possible using Firebase's sendEmailVerification. In addition to sending this email, I want the user's account to be disabled until they verify the email. This would also require using Firebase's isEmailVerified feature. However, I have been unsuccessful in getting Firebase to send the verification email, I have not been able to figure out to get it to disable and enable the account sending the verification email and after it has been verified.

推荐答案

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

This question is about how to use Firebase to send the verification email. The OP is unable to figure out how to disable and enable the account sending the verification email and after it has been verified.

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

Also, this is not properly documented in the firebase documentation. So I am writing a step by step procedure that someone may follow if he/she is facing the problem.

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());

                        }
                    }
                });
    }

现在进入 LoginActivity:

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

Here if the user is successfully logged in then we can simply call a method where you are writing logic for checking if the email is verified or not.

示例:

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

    }
}

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

So here I m checking if the email is verified or not. If not, then log out the user.

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

So this was my approach to keeping track of things properly.

这篇关于如何使用 Firebase 发送验证电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆