如何向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());

                        }
                    }
                });
    }

现在来到LoginActivity:

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

例:

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

    }
}

所以我在这里检查电子邮件是否被验证。 如果不是,则退出该用户。

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


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

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


向用户的电子邮件发送验证

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

检查用户是否被验证

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
boolean emailVerified = user.isEmailVerified();
链接地址: http://www.djcxy.com/p/74051.html

上一篇: How to send verification email with Firebase?

下一篇: Transfer registered users from one firebase app to another