Firebase email verification workflow

若如初见. 提交于 2019-12-20 07:12:17

问题


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?


回答1:


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.

According to the docs:

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( .... )
}



回答2:


FYI if you are using Firebase email verification think about testing it during your end-to-end or smoke tests.

Use a service like emaile2e.com to generate random email addresses during a test which you can send and receive from. That way you can verify users during a test programmatically.



来源:https://stackoverflow.com/questions/40729486/firebase-email-verification-workflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!