javascript - Firebase v3 密码身份验证 - 触发/Fire 验证电子邮件?

标签 javascript firebase firebase-authentication

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

那么,如何触发密码验证?如何获取 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;
}

最佳答案

firebase.auth().currentUser.sendEmailVerification() 发送 modeoobCodeapiKey 登录用户电子邮件地址的变量。在经过验证之前,可以通过读取 currentUser.emailVerified 属性将登录用户标识为未经验证。用户打开电子邮件并单击验证链接,该链接将调用到您的身份验证监听器页面的 HTTP session ,其中这些变量作为 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很有用。其他一些相关方法(全部可用)如下所示:

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

关于javascript - Firebase v3 密码身份验证 - 触发/Fire 验证电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38545493/

相关文章:

firebase - Flutter:显示来自经过身份验证的 Firebase Auth 用户的带有 StreamBuilder 的一些 Firestore 集合

reactjs - React Native Firebase 验证码

javascript - 如何模拟 firebase.auth.UserCredential Jest ?

javascript - firebase 文档引用 grecaptcha 但从不导入或定义它

android - firebase 重复用户名和自定义登录

android - 为什么 Firebase pendingAuthResult 返回 null?

javascript - 无法使用 JavaScript 获取宽度属性

javascript - 是否有针对强类型语言的 promise 规范?

javascript - 将数据保存到 Syncano Angular 应用程序

javascript - 获取点击进度条部分的宽度并计算秒数