【问题标题】:Firebase reset password when email is not exist电子邮件不存在时 Firebase 重置密码
【发布时间】:2017-10-15 13:45:18
【问题描述】:

我正在开发一个带有 Firebase 用户身份验证的 Android 应用程序。我面临的问题是我从用户那里获取电子邮件和密码,然后将该用户创建到 Firebase 中。我没有验证用户输入的电子邮件。现在我想实现重置密码功能。为此,Firebase 提供了 resetPassword 方法并向该特定用户发送重置密码电子邮件。但问题是,如果电子邮件不存在,我们该怎么办?

这是我用来在 Firebase 中注册用户的代码:

private void registerUser(){

        //creating a new user
        firebaseAuth.createUserWithEmailAndPassword("user email here", "user password here")
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        //checking if success
                        if(task.isSuccessful()){
                            //display some message here 
                        }else{
                            //display some message here 
                        }

                    }
                });

    }

如果有任何替代选项可用于此功能,请告诉我。 谢谢。

【问题讨论】:

    标签: android firebase firebase-authentication forgot-password


    【解决方案1】:

    另一种方法是使用 Firebase Admin SDK 更改用户密码。来自documentation on updating user information

    updateUser() 方法允许您修改现有用户的数据。它接受一个uid 供用户更新,以及一个包含UserRecord 属性的对象来更新:

    admin.auth().updateUser(uid, {
      email: "modifiedUser@example.com",
      emailVerified: true,
      password: "newPassword",
      displayName: "Jane Doe",
      photoURL: "http://www.example.com/12345678/photo.png",
      disabled: true
    })
      .then(function(userRecord) {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully updated user", userRecord.toJSON());
      })
      .catch(function(error) {
        console.log("Error updating user:", error);
      });
    

    与:

    password- string - 用户的新原始未散列密码。长度必须至少为六个字符。

    Firebase Admin SDK 的这一部分目前仅在 Node.js 中可用。但是如果你还没有 Node.js 服务器,你可以实现Cloud Functions for Firebase 中的功能。

    【讨论】:

      【解决方案2】:

      请尝试下面的代码可能对你有帮助,我正在使用这个。

      private FirebaseUser user;
      user = FirebaseAuth.getInstance().getCurrentUser();
      final String email = user.getEmail();
      AuthCredential credential = EmailAuthProvider.getCredential(email,oldpass);
      user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
      @Override
       public void onComplete(@NonNull Task<Void> task) {
          if(task.isSuccessful()){
               user.updatePassword(newPass).addOnCompleteListener(new OnCompleteListener<Void>() {
                   @Override
                   public void onComplete(@NonNull Task<Void> task) {
                       if(!task.isSuccessful()){
                           Snackbar snackbar_fail = Snackbar
                                             .make(coordinatorLayout, "Something went wrong. Please try again later", Snackbar.LENGTH_LONG);
                                               snackbar_fail.show();
                         }else {
                            Snackbar snackbar_su = Snackbar
                                                .make(coordinatorLayout, "Password Successfully Modified", Snackbar.LENGTH_LONG);
                                                  snackbar_su.show();
                                              }
                                          }
                                      });
                         }else {
                                  Snackbar snackbar_su = Snackbar
                                          .make(coordinatorLayout, "Authentication Failed", Snackbar.LENGTH_LONG);
                                  snackbar_su.show();
                              }
                          }
                      });
                  }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-15
        • 2013-12-18
        • 2017-02-26
        • 1970-01-01
        • 2019-07-21
        相关资源
        最近更新 更多