【发布时间】:2018-01-01 15:09:38
【问题描述】:
我正在使用 sdk 为该用户创建一个用户和一个数据库条目,这一切都可以完美运行。在创建数据库条目后,我调用了一个函数 sendEmailVerification() 但我猜这是一个客户端函数,因为它在被调用时返回 null。
直接从 admin sdk 发送验证电子邮件的过程是什么(如果可能的话)。目前我所做的是将一些 JSON 发送回客户端,以说明验证电子邮件是否发送成功。但是调用该函数不起作用,所以它没有那么远。这是我在节点中的函数。
function verifiyEmail(email, res) {
var user = admin.auth().currentUser;
user.sendEmailVerification().then(function() {
// Email sent.
var jsonResponse = {
status: 'success',
alertText: '1',
email: email
}
res.send(jsonResponse);
}, function(error) {
// An error happened.
var jsonResponse = {
status: 'success',
alertText: '0',
email: email
}
res.send(jsonResponse);
});
}
更新
我猜这是不可能的,所以我在节点中生成了一个自定义令牌并将其发送回客户端。然后,我使用返回的令牌尝试通过调用以下命令登录用户,但不会调用 signInWithCustomToken() 函数。这是我的代码我错过了什么。发送验证邮件似乎需要做很多工作!
function signInUserWithToken(token) {
firebase.auth().signInWithCustomToken(token).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
verifiyEmail();
});
}
更新 2
我放弃了令牌的想法。我现在所做的就是使用 onAuthStateChanged() 函数并在客户端实现中处理电子邮件验证。它并不完美,因为此方法被多次调用。但是,添加标志似乎可以解决问题。如下所示。
function authListenerContractor() {
// Listening for auth state changes.
$('#not-verified').css('display','none');
var flag = true;
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is verified.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
console.log("Email Verified?: " + emailVerified);
if(emailVerified) {
window.location.href = "http://www.my-redirect-url.com";
} else {
if (flag == true) {
$('#not-verified').css('display','inherit');
verifiyEmail();
flag = false;
}
}
} else {
console.log("User is signed out.");
}
});
}
function verifiyEmail() {
var user = firebase.auth().currentUser;
user.sendEmailVerification().then(function() {
// Email sent.
console.log("Verification email sent");
$('#not-verified').text('**Email verification sent. Please check your email now!**');
}, function(error) {
// An error happened.
console.log("Email verification not sent. An error has occurred! >>" + error);
});
}
【问题讨论】:
-
您想在 firebase 数据库中创建某些内容后向用户发送验证吗?
-
@RahulSingh 是的,没错。
-
您正在使用 Firebase 云功能?
-
@RahulSingh 不确定我是否关注。我只使用 node.js firebase 管理模块。
-
Node.js Admin SDK 中存在一个请求此功能的未解决问题:github.com/firebase/firebase-admin-node/issues/46。留意那张票可能会很有用。
标签: node.js firebase firebase-authentication firebase-admin