简体   繁体   English

Firebase电子邮件验证工作流程

[英]Firebase email verification workflow

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. 然后,在用户邮件客户端中通过验证并单击链接之后,我希望将该用户添加到我的Firebase用户中。

At the moment, I am just testing without any email sending via SMTP, just adding data to Firebase. 目前,我只是在测试没有通过SMTP发送任何电子邮件,只是将数据添加到Firebase。 However, no emails I add are being added to my Firebase database. 但是,我添加的电子邮件没有添加到Firebase数据库中。

Current code in the bottom of the body of the HTML, before the other script tags (should this be in the head?): 当前代码在HTML主体的底部,在其他脚本标签之前(应该在头部吗?):

<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 : 这是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. 我知道在尝试一个简单的JS警报时会调用signup功能,当单击“提交”按钮时,该警报确实会弹出。 However, I am seeing nothing change in my Firebase data dashboard under the /signups section. 但是,我在/signups部分下的Firebase数据仪表板中没有看到任何更改。 Also, the URL changes to: 此外,URL更改为:

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? 没有通过SendGrid等进行电子邮件确认的现有设置怎么了?

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. 然后,在用户邮件客户端中通过验证并单击链接之后,我希望将该用户添加到我的Firebase用户中。

I do not think Firebase works that way. 我认为Firebase不能那样工作。 Unless the user is registered, you cannot send a verification email to an arbitrary email address. 除非用户注册,否则您无法将验证电子邮件发送到任意电子邮件地址。 The sendEmailVerification method works on a currentUser . sendEmailVerification方法适用于currentUser

According to the docs : 根据文档

You can send an address verification email to a user with the sendEmailVerification method 您可以使用sendEmailVerification方法向用户发送地址验证电子邮件

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 使用最新的SDK,您可以按照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( .... )
}

FYI if you are using Firebase email verification think about testing it during your end-to-end or smoke tests. 仅供参考,如果您使用Firebase电子邮件验证,请考虑在端到端或冒烟测试期间对其进行测试。

Use a service like emaile2e.com to generate random email addresses during a test which you can send and receive from. 在测试期间,可以使用诸如emaile2e.com之类的服务生成随机的电子邮件地址,以便您发送和接收。 That way you can verify users during a test programmatically. 这样,您可以以编程方式在测试期间验证用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM