对于 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()
发送 mode
、oobCode
和 apiKey
登录用户电子邮件地址的变量。在经过验证之前,可以通过读取 currentUser.emailVerified
属性将登录用户标识为未经验证。用户打开电子邮件并单击验证链接,该链接将调用到您的身份验证监听器页面的 HTTP session ,其中这些变量作为 URL 中的参数出现。
使用我原来的帖子(或您自己的帖子)中的 getArg()
方法将浏览器 window.location
(当前网址/URL)中的参数解析为身份验证监听器页面 JavaScript 中的变量。
- 构建您的身份验证监听器页面 ( mostly paste from docs )
向登录用户发送电子邮件
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/