【问题标题】:Firebase handle reset password emails for not verified usersFirebase 为未经验证的用户处理重置密码电子邮件
【发布时间】:2018-02-23 19:45:41
【问题描述】:

当新用户注册 Web 应用程序时,会向他发送一封验证电子邮件。我阻止新用户在验证前登录。

同时,如果验证链接过期并且用户忘记了密码,他将单击重置密码链接并收到一封电子邮件。

所以我认为我应该同时处理重置密码操作和验证。否则用户更改密码后也无法登录。

function handleResetPassword(auth, actionCode) {
    auth.verifyPasswordResetCode(actionCode)
       .then(function (email) {
          // Showing the reset screen and ask the user for
          // the new password.
       }).catch(function (error) {
         //
       });
};

当用户保存新密码时:

function saveNewPassword() {
    auth.confirmPasswordReset(actionCode, vm.form.password).then(function (resp) {
        // Password reset has been confirmed and new password updated.
        // Now auto sign in user
        auth.signInWithEmailAndPassword(vm.email, vm.form.password).catch(function (error) {
            // Handle Errors here.
        });

        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                // user signed in. 
                // check whether the user is verified
                // if not set true
                user.updateProfile({ emailVerified: true })
            }
        });

    }).catch(function (error) {
        // 

    });
}

但是下面的代码没有按我的预期工作,因为它没有任何影响。我可以更改其他用户数据(例如 displayName),但不能更改(emailVerified)。它仅适用于 Firebase 电子邮件验证。

user.updateProfile({ emailVerified: true })

这种类型的用户场景推荐的方法是什么?

【问题讨论】:

    标签: angularjs firebase firebase-authentication


    【解决方案1】:

    您不能从客户端更新emailVerified,否则任何未经验证的用户都可以在不强制执行电子邮件的实际所有权的情况下执行此操作。 您需要使用 Admin SDK 使用 HTTP 端点来执行此操作(您也可以使用 Firebase 函数)。但是,您需要确保密码重置代码成功。所以在这种情况下,您需要在服务器上运行您的代码。以下是它的工作原理:

    var firebase = require('firebase');
    var admin = require('firebase-admin');
    // Initialize the client and admin instances.
    // firebase.initializeApp(clientConfig);
    // admin.initializeApp(adminConfig);
    // Send the reset code and the new password to your backend. 
    var email = null;
    // Get email corresponding to code.
    firebase.auth().checkActionCode(actionCode)
      .then(function(info) {
        email = info.email;
        // Confirm password reset.
        firebase.auth().confirmPasswordReset(actionCode, password)
      });
      .then(function() {
        // Get uid of user with corresponding email.
        return admin.auth().getUserByEmail(email);
      }).then(function(userRecord) {
        // Password reset succeeded. Email can be verified as the user
        // must have received the code via their email confirming
        // ownership.
        return admin.auth().updateUser(userRecord.uid, {emailVerified: true});
      });
    

    【讨论】:

    • 目前我使用与 firebase 文档中描述的相同的自定义电子邮件处理程序。我没有后端服务器。我的网络应用托管在 firebase 上。我搜索了 Firebase 云功能来处理这个问题,但我做不到。我认为您的示例代码不适用于云功能?
    • 您需要使用 Firebase 函数构建一个 HTTP 端点来处理这个问题。查看文档以了解更多如何执行此操作:firebase.google.com/docs/functions/http-events
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2016-02-11
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多