Firebase电子邮件验证工作流程 [英] Firebase email verification workflow
问题描述
我有一个简单的输入框,要求用户发送电子邮件。
我希望他们输入一些东西,让我检查它是一个字符串,然后发送一个验证电子邮件到他们输入的电子邮件地址。
然后,一旦在用户邮件客户端中进行了验证,并且链接被点击,我希望将用户添加到我的Firebase用户。 >
目前,我只是通过SMTP进行任何电子邮件发送测试,只需向Firebase添加数据即可。但是,我添加的电子邮件没有添加到我的Firebase数据库中。
HTML的正文底部的当前代码,在其他脚本标签之前(应该是在头上?):
< script src =https://www.gstatic.com/firebasejs/3.6。 1 / firebase.js>< /脚本>
< script>
//初始化Firebase
var config = {
apiKey:myapikey,
authDomain:mydomain.firebaseapp.com,
databaseURL:https: /mydomain.firebaseio.com,
storageBucket:mydomain.appspot.com,
messagingSenderId:mymessagesenderid
};
firebase.initializeApp(config);
< / script>
< script src =assets / js / saveEmail.jstype =text / javascript>< / script>
然后我有一个输入框:
< div class =mtb>
< h2 class =signup-title>注册我们项目的更新< / h2>
< p id =signup-successclass =text-success>< / p>
< p id =signup-errorclass =text-danger>< / p>
< form class =signup-form form-inlineid =signup-formrole =formonsubmit =return signup(this)>
< div id =div>
< input type =emailname =emailclass =subscribe-inputplaceholder =输入您的电子邮件地址...必需>
< button class ='btn btn-conf btn-yellow'id =signup-buttontype =submit> Submit
< / button>
< / div>
< / form>
< / div>
这是 saveEmail.js
:
var signupForm = document.getElementById('signup-form');
var signupSuccess = document.getElementById('signup-success');
var signupError = document.getElementById('signup-error');
var signupBtn = document.getElementById('signup-button');
var onSignupComplete = function(error){
signupBtn.disabled = false;
if(error){
signupError.innerHTML ='对不起。无法注册。
} else {
signupSuccess.innerHTML =感谢您注册!;
//隐藏窗体
signupForm.style.display ='none';
}
};
函数注册(formObj){
//将电子邮件存储到firebase
var myFirebaseRef = new Firebase(https://mydomain.firebaseio.com/signups);
myFirebaseRef.push({
email:formObj.email.value,
},onSignupComplete);
signupBtn.disabled = true;
返回false;
}
我知道注册
函数正在被调用,因为我尝试了一个简单的JS警报,当单击提交按钮时,它会弹出。但是,在我的Firebase数据仪表板中,在 / signups
部分中看不到任何变化。此外,网址更改为:
http:// localhost:5000 /?email = theemailthatwasputintothebox
$我修改了我的规则: {rules:
{.read:true,
.write:true
}
}
所以我认为这不是关于规则,因为我已经启用了所有的测试目的。
如何实现我想要达到的目标(没有,然后与电子邮件确认部分)?
现有设置出现了什么问题,没有通过SendGrid等电子邮件确认?
解决方案
我有一个简单的输入框要求用户的电子邮件。
我希望他们输入一些东西,让我检查它是一个字符串,然后
发送验证电子邮件到他们输入的电子邮件地址。
然后一旦在用户邮件客户端中验证并链接被点击,希望将用户添加到我的Firebase用户。
我不认为Firebase以这种方式工作。除非用户注册,否则您无法将验证电子邮件发送到任意电子邮件地址。 sendEmailVerification
方法适用于 currentUser
。
< a href =https://firebase.google.com/docs/auth/web/manage-users#send_a_user_a_verification_email =nofollow noreferrer>根据文档:
您可以使用sendEmailVerification方法向用户发送地址验证电子邮件
现在,对于你的代码段,你不是简单地尝试用户输入并将其保存到数据库中?使用最近的SDK,您可以尝试这样做,根据文档
....
var database = firebase.database();
函数注册(formObj){
//将电子邮件存储到firebase
database.ref('signups')
.push({
email:
})
.then(function(res){
console.log(res)
//调用你想要的任何回调
})
.catch(....)
}
I have a simple input box asking for users emails.
I want them to input something, for me to check it is a string, and then send a verification email to their email address entered.
Then once verified within users mail client and the link is clicked, I want the user to be added to my Firebase users.
At the moment, I am just testing without any email sending via SMTP, just adding data to Firebase. However, no emails I add are being added to my Firebase database.
Current code in the bottom of the body of the HTML, before the other script tags (should this be in the head?):
<script src="https://www.gstatic.com/firebasejs/3.6.1/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "myapikey",
authDomain: "mydomain.firebaseapp.com",
databaseURL: "https://mydomain.firebaseio.com",
storageBucket: "mydomain.appspot.com",
messagingSenderId: "mymessagesenderid"
};
firebase.initializeApp(config);
</script>
<script src="assets/js/saveEmail.js" type="text/javascript"></script>
Then I have an input box:
<div class="mtb">
<h2 class="signup-title">Sign up for updates on our project</h2>
<p id="signup-success" class="text-success"></p>
<p id="signup-error" class="text-danger"></p>
<form class="signup-form form-inline" id="signup-form" role="form" onsubmit="return signup(this)">
<div id="div">
<input type="email" name="email" class="subscribe-input" placeholder="Enter your e-mail address..." required>
<button class='btn btn-conf btn-yellow' id="signup-button" type="submit">Submit
</button>
</div>
</form>
</div>
And this is saveEmail.js
:
var signupForm = document.getElementById('signup-form');
var signupSuccess = document.getElementById('signup-success');
var signupError = document.getElementById('signup-error');
var signupBtn = document.getElementById('signup-button');
var onSignupComplete = function (error) {
signupBtn.disabled = false;
if (error) {
signupError.innerHTML = 'Sorry. Could not signup.';
} else {
signupSuccess.innerHTML = 'Thanks for signing up!';
// hide the form
signupForm.style.display = 'none';
}
};
function signup(formObj) {
// Store emails to firebase
var myFirebaseRef = new Firebase("https://mydomain.firebaseio.com/signups");
myFirebaseRef.push({
email: formObj.email.value,
}, onSignupComplete);
signupBtn.disabled = true;
return false;
}
I know that the signup
function is being called as I tried a simple JS alert, which does pop up when the submit button is clicked. However, I am seeing nothing change in my Firebase data dashboard under the /signups
section. Also, the URL changes to:
http://localhost:5000/?email=theemailthatwasputintothebox
I modified my rules to:
{"rules":
{".read": true,
".write": true
}
}
So I assume this is not about rules as I have enabled everything for testing purposes.
How can I achieve what I want to achieve (without, and then with the email confirmation part)?
What is going wrong with the existing setup, without email confirmation via SendGrid etc?
解决方案
I have a simple input box asking for users emails.
I want them to input something, for me to check it is a string, and
then send a verification email to their email address entered.
Then once verified within users mail client and the link is clicked, I want the user to be added to my Firebase users.
I do not think Firebase works that way. Unless the user is registered, you cannot send a verification email to an arbitrary email address. The sendEmailVerification
method works on a currentUser
.
You can send an address verification email to a user with the sendEmailVerification method
Now, to your snippet, aren't you simply trying to take a user input and save it to the database? With the recent SDK, you can try this, as per docs
....
var database = firebase.database();
function signup(formObj) {
// Store emails to firebase
database.ref('signups')
.push({
email: formObj.email.value,
})
.then(function(res) {
console.log(res)
// call whatever callback you want here
})
.catch( .... )
}
这篇关于Firebase电子邮件验证工作流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!