如何通过Firebase发送验证邮件? [英] How to send verification email with Firebase?

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

问题描述

我正在使用Firebase的电子邮件和密码方法注册用户。像这样:
$ b $ pre $ 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());

android.os.Handler() .postDelayed(

new Runnable(){
public void run(){

updateUser(b);

}



$ new b




$ b $ new $
public void run(){

onSignupFailed();

$ b},3000);

}

}
});

用户的电子邮件成功注册后,我希望Firebase发送验证邮件。我知道这可以使用Firebase的 sendEmailVerification 。除了发送此电子邮件之外,我还希望在验证电子邮件之前禁用用户的帐户。这也需要使用Firebase的 isEmailVerified 功能。不过,我一直未能让Firebase发送验证电子邮件,但我一直无法弄清楚它是否禁用并启用发送验证电子邮件的帐户并验证通过。

解决方案

这个问题是关于如何使用Firebase发送验证邮件。 OP无法弄清楚如何禁用和启用发送验证邮件的帐户并验证通过。

另外,在firebase中没有正确记录文档。所以我写了一个一步一步的过程,有人可能会跟随,如果他/她面临的问题。
$ b <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());

//如果登录失败,向用户显示一条消息,如果登录成功
// auth状态侦听器将被通知,逻辑来处理
//登录用户可以在侦听器中处理。
if(!task.isSuccessful()){
//显示m essage task.getException()
}
else
{
//成功帐户创建
//现在AuthStateListener运行onAuthStateChanged回调
}

// ...
}
});

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


$ b $ pre $ onCreate(... //
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser(); $ b $ if(user!= null){
//用户登录
/ /注意:只有当用户没有登录时,这个活动才会得到onpen,否则
//用户将收到另一个验证邮件
sendVerificationEmail();
} else {
//用户退出





code $ $ b

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

  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()){
//电子邮件发送


//发送电子邮件后,只需注销用户并完成此活动
startActivity(new Intent(SignupActivity.this,LoginActivity.class));
finish();
}
else $ $ b $ FireBaseAuth.getInstance()。signOut b $ b {
//电子邮件没有发送,所以显示消息并重新启动活动或做任何你想做的事

//重新启动这个动作ivity
overridePendingTransition(0,0);
finish();
overridePendingTransition(0,0);
startActivity(getIntent());

}
}
});
}

现在进入LoginActivity:



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



示例:

  mAuth.signInWithEmailAndPassword(email,password)$ b $ (@NonNull Task< AuthResult>任务){
//Log.d(TAG ,signInWithEmail:onComplete:+ task.isSuccessful());

//如果登录失败,向用户显示一条消息,如果登录成功
// auth状态监听器将被通知,并且处理
//登录用户的逻辑可以在监听器中处理。
if(!task.isSuccessful()){
//Log.w(TAG,signInWithEmail:failed,task.getException());

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

现在考虑checkIfEmailVerified方法:

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

if(user.isEmailVerified())
{
//用户已验证,因此您可以完成此活动或将用户发送到您想要的活动。
finish();
Toast.makeText(LoginActivity.this,成功登录,Toast.LENGTH_SHORT).show();
}
else
{
//电子邮件没有验证,所以只要提示消息给用户并重新开始这个活动。
//注意:不要忘记注销用户。
FirebaseAuth.getInstance()。signOut();

//重新启动此活动


$ b code $ pre

所以我在这里检查电子邮件是否被验证。如果没有,那么退出用户。

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


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

    }

    }
});

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.

解决方案

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.

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) User can use createUserWithEmailAndPassword method.

Example:

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
                        }

                        // ...
                    }
                });

If the new account was created, the user is also signed in, and the AuthStateListener runs the onAuthStateChanged callback. In the callback, you can manage the work of sending the verification email to the user.

Example:

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

        }
        // ...
    }
};

Now the send verification email can be written like:

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

                        }
                    }
                });
    }

Now coming to 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.

Example:

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();
                        }
                        // ...
                    }
                });

Now consider the checkIfEmailVerified method:

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天全站免登陆