2017-09-14 28 views
0

當新用戶註冊Web應用程序時,會向他發送驗證電子郵件。我阻止新用戶在驗證之前登錄。未驗證用戶的Firebase句柄重置密碼電子郵件

與此同時,如果驗證鏈接到期並且用戶忘記密碼,他將單擊重置密碼鏈接並收到電子郵件。

所以我認爲我應該一起處理重置密碼操作和驗證。否則,即使更改密碼後,用戶也將無法登錄。

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

您不能從客戶端更新emailVerified,否則任何未經驗證的用戶都可以在不強制電子郵件的實際所有權的情況下執行此操作。 您需要使用HTTP端點(使用Firebase功能)使用Admin SDK來執行此操作。但是,您需要確保密碼重置代碼成功。所以在這種情況下,你需要在服務器上運行你的代碼。這是它將如何工作:

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}); 
    }); 
+0

目前我使用自定義電子郵件處理程序相同的firebase文檔中描述。我沒有後端服務器。我的網站應用託管在firebase上。我搜索了firebase雲功能來處理這個,但我不能。我認爲你的示例代碼不適用於雲功能? –

+0

您需要使用Firebase函數構建HTTP端點來處理此問題。查看文檔以瞭解更多信息,方法如下:https://firebase.google.com/docs/functions/http-events – bojeil

相關問題