Firebase会为未经过验证的用户重置密码电子邮件

时间:2017-09-14 18:01:44

标签: angularjs firebase firebase-authentication

当新用户注册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 })

此类用户场景的推荐方法是什么?

1 个答案:

答案 0 :(得分:1)

您无法从客户端更新emailVerified,否则任何未经验证的用户都可以在不强制执行电子邮件的实际所有权的情况下执行此操作。 您需要使用HTTP端点使用Admin SDK执行此操作(您也可以使用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});
  });