2016-07-23 100 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()