【问题标题】:How to send verification email with Firebase?如何使用 Firebase 发送验证电子邮件?
【发布时间】:2017-03-17 05:15:00
【问题描述】:

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

【问题讨论】:

标签: android firebase firebase-authentication


【解决方案1】:

对于科特林

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

【讨论】:

    【解决方案2】:
       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();
                                }
                            }
                        });
    

    【讨论】:

      【解决方案3】:

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

      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

      【讨论】:

      • 任何线索为什么这个错误只在 ANDROID 上? E/JS: ERROR 错误: java.lang.NullPointerException: 指定为非空的参数为空: 方法 kotlin.jvm.internal.Intrinsics.checkNotNullParameter @nativescript/firebase-auth/index.android.js:275:0) JS :at ZoneAwarePromise(,,zone.js/fesm2015/zone.js:1387:0) JS:at sendEmailVerification(...@nativescript/firebase-auth/index.android.js:270:0) stackoverflow.com/questions/70916701/…跨度>
      【解决方案4】:

      发送验证到用户的邮箱

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

      检查用户是否通过验证

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

      【讨论】:

      【解决方案5】:

      这个问题是关于如何使用 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
      
          }
      }
      

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

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

      【讨论】:

      • 根据您的代码,如何检查电子邮件是否已通过验证?它将成功登录。但是你应该先验证电子邮件,然后你必须登录,这是正确的方法。你能帮我找到正确的方法吗?
      • @Simon 我回答的最后一个代码 sn-p 正在做你想做的事。让用户登录,然后检查他/她的电子邮件是否经过验证。如果未验证电子邮件,则退出用户。
      • 我都做了,验证邮件发送了,我点击了链接,但是 user.isEmailVerified() 不起作用。
      • 我已经找到了答案。它的工作真棒,谢谢你的帮助......
      【解决方案6】:

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

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

      【讨论】:

      • 这是解决方案的一部分,但没有回答我的问题。
      • 谢谢。我认为我将要使用的解决方法是创建一个默认密码,然后发送密码重置。通知用户首次登录时先设置新密码。
      猜你喜欢
      • 2020-12-12
      • 2019-06-24
      • 2018-11-21
      • 1970-01-01
      • 2019-03-13
      • 2020-08-06
      • 2017-09-09
      • 2020-10-10
      • 2020-09-08
      相关资源
      最近更新 更多