Firebase v3密码验证 - 触发/防火验证电子邮件?

时间:2016-07-23 19:05:44

标签: javascript firebase firebase-authentication

对于Firebase上托管的Web应用程序,我想通过向他们发送验证链接来验证新用户。我浏览了文档Create custom email action handlers,并配置了我的自定义域,并使密码验证工作正常。

那么,我该如何触发密码验证呢?我如何获得oobCode和apiKey参数?我正在注册一个新用户,我的auth listener页面上没有任何反应?

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;
}

1 个答案:

答案 0 :(得分:1)

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
    
  3. Official Web Methods Reference Index很有用。其他一些相关的方法(都是可以的)如下所示:

    //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()