2016-07-23 102 views
0

對於Firebase上託管的Web應用程序,我想通過向他們發送驗證鏈接來驗證新用戶。我瀏覽了文檔Create custom email action handlers,並配置了我的自定義域並使密碼驗證正常工作。Firebase v3密碼驗證 - 觸發器/消防驗證電子郵件?

那麼,我該如何觸發密碼驗證?我如何獲得oobCode和apiKey參數?我正在註冊一個新用戶,並且在我的驗證監聽器頁面上沒有任何反應?

var getArg = getArg(); 
var mode = getArg['mode']; 
var oobCode = getArg['oobCode']; 
var apiKey = getArg['apiKey']; 

console.log(mode, oobCode, apiKey); 

switch (mode) { 
    case 'resetPassword': 
     console.log('Password Request Fired'); 
     break; 
    case 'recoverEmail': 
     console.log('Recover Email Fired'); 
     break; 
    case 'verifyEmail': 
     console.log('Verify Email Fired'); 
     break; 
} 

//Thanks Geoffrey Crofte 
function getArg(param) { 
    var vars = {}; 
    window.location.href.replace(location.hash, '').replace(
     /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp 
     function(m, key, value) { // callback 
      vars[key] = value !== undefined ? value : ''; 
     } 
    ); 

    if (param) { 
     return vars[param] ? vars[param] : null; 
    } 
    return vars; 
} 
+0

看到這裏,它可能揭示你可以做一些輕:http://stackoverflow.com/documentation/proposed/changes/35793 – Rexford

+0

@Rexford謝謝! –

+0

@雷克斯福德:請提供必要的信息作爲答案。否則,我們只能通過鏈接到文檔的註釋獲得未解答的問題。 –

回答

0

firebase.auth().currentUser.sendEmailVerification()發送modeoobCodeapiKey變量的用戶的電子郵件地址登錄。在驗證之前,通過閱讀currentUser.emailVerified屬性,可以將登錄用戶標識爲未驗證。用戶打開電子郵件並單擊驗證鏈接,該鏈接將調用HTTP會話到您的驗證監聽器頁面,並將這些變量作爲URL中的參數存在。

在我的原始文章(或您自己的文章)中使用getArg()方法將參數從瀏覽器window.location(當前網址/ URL)解析到您的驗證監聽器頁面的JavaScript變量中。

  1. 打造出您的身份驗證監聽器頁面(mostly paste from docs
  2. 登錄的用戶的電子郵件

    firebase.auth().currentUser.sendEmailVerification() 
    //checks if signed in user is verified, if not tell them to check email 
    firebase.auth().currentUser.emailVerified; //false now so handle user experience appropriately until verified 
    

Official Web Methods Reference Index是有用的發送。其他一些相關方法(所有thenable)如下所示:

//updates email of signed in user 
-> firebase.auth().currentUser.updateEmail(newEmail) 

//updates password of signed in user 
-> firebase.auth().currentUser.updatePassword(newPassword) 
-> firebase.auth().sendPasswordResetEmail(email) 

//deletes auth account of signed in user 
-> firebase.auth().currentUser.delete()