Tutorial or working example of sending verification emails with firebase auth web

1,161 views
Skip to first unread message

Erin B

unread,
May 17, 2017, 9:52:58 PM5/17/17
to Firebase Google Group
Hello, I saw there was a long thread about this but I'm still unsure how exactly to send verification emails. Has anyone found a working example or tutorial on how to send verification emails? I saw the code in the docs (https://firebase.google.com/docs/auth/web/manage-users) but I'm unsure of where exactly to put it within my Js file. Here is my code so far:



"use strict";
(function () {
    // initialize firebase
    var config = {
        apiKey: "",
        authDomain: "",
        databaseURL: "",
        projectId: "",
        storageBucket: "",
        messagingSenderId: ""
    };
    firebase.initializeApp(config);
    // get the login form elements from HTML
    var txtEmail = document.getElementById('txtEmail');
    var txtPassword = document.getElementById('txtPassword');
    var btnLogin = document.getElementById('btnLogin');
    var btnSignUp = document.getElementById('btnSignUp');
    var btnLogout = document.getElementById('btnLogout');
    // add login event
    btnLogin.addEventListener('click', function (e) {
        // get email and pass
        var email = txtEmail.value;
        var pass = txtPassword.value;
        var auth = firebase.auth();
        // sign in 
        var promise = auth.signInWithEmailAndPassword(email, pass);
        promise.catch(function (e) {
            return console.log(e.message);
        });
    });
    // add signup event. the signup button sends user email and pass to firebase
    btnSignUp.addEventListener('click', function (e) {
        // get email and pass
        // TODO: validate email - check it is real
        var email = txtEmail.value;
        var pass = txtPassword.value;
        var auth = firebase.auth();
        // sign in 
        var promise = auth.createUserWithEmailAndPassword(email, pass);
        promise.catch(function (e) {
            return console.log(e.message);
        });
    });
    // show logout button when user is logged in
    // TODO: make sure the login form clears the credentials on logout
    btnLogout.addEventListener('click', function (e) {
        firebase.auth().signOut();
    });
    // realtime authentication listener
    firebase.auth().onAuthStateChanged(function (firebaseUser) {
        if (firebaseUser) {
            console.log(firebaseUser);
            btnLogout.classList.remove('hide');
            btnSignUp.classList.add('hide');
            btnLogin.classList.add('hide');
        } else {
            console.log('not logged in');
            btnLogout.classList.add('hide');
            btnSignUp.classList.remove('hide');
            btnLogin.classList.remove('hide');
        } // end if statement
    }); // end function
})();


Thank you :)

Bassam

unread,
May 18, 2017, 4:33:56 AM5/18/17
to Firebase Google Group
Hey Erin B, I provided an answer to your stackoverflow question:

Bassam

Erin B

unread,
May 18, 2017, 10:58:55 AM5/18/17
to Firebase Google Group
Hi Bassam. Thank you for your reply. I really appreciate it. Got it working. Here's a code update:

"use strict";

(function () {

   
// initialize firebase
   
var config = {
        apiKey
: "",
        authDomain
: "",
        databaseURL
: "",
        projectId
: "",
        storageBucket
: "",
        messagingSenderId
: ""
   
};
    firebase
.initializeApp(config);

   
// get the login form elements from HTML
   
var txtEmail = document.getElementById('txtEmail');
   
var txtPassword = document.getElementById('txtPassword');
   
var btnLogin = document.getElementById('btnLogin');
   
var btnSignUp = document.getElementById('btnSignUp');
   
var btnLogout = document.getElementById('btnLogout');


   
// login event

    btnLogin
.addEventListener('click', function (e) {
       
// get email and pass
       
var email = txtEmail.value;
       
var pass = txtPassword.value;
       
var auth = firebase.auth();
       
// sign in
       
var promise = auth.signInWithEmailAndPassword(email, pass);
        promise
.catch(function (e) {
           
return console.log(e.message);
       
});
   
});

   
// add signup event. the signup button sends user email and pass to firebase
    btnSignUp
.addEventListener('click', function (e) {
       
// get email and pass
       
// TODO: validate email - check it is real
       
var email = txtEmail.value;
       
var pass = txtPassword.value;
       
var auth = firebase.auth();

       
var user = firebase.auth().currentUser;

       
// create user and sign in

       
var promise = auth.createUserWithEmailAndPassword(email, pass);

        promise
.then(function(user) {// You are forgetting this reference.
          user
.sendEmailVerification().then(function() {
         
// Email sent.
       
}, function(error) {
         
// An error happened.
       
});
   
});  // end promise
   
}); // end sign up button event listener


   
// show logout button when user is logged in
   
// TODO: make sure the login form clears the credentials on logout
    btnLogout
.addEventListener('click', function (e) {
        firebase
.auth().signOut();
   
});

   
// realtime authentication listener
    firebase
.auth().onAuthStateChanged(function (firebaseUser) {
       
if (firebaseUser) {
            console
.log(firebaseUser);
            btnLogout
.classList.remove('hide');
            btnSignUp
.classList.add('hide');
            btnLogin
.classList.add('hide');
       
} else {
            console
.log('not logged in');
            btnLogout
.classList.add('hide');
            btnSignUp
.classList.remove('hide');
            btnLogin
.classList.remove('hide');

       
} // end else statement
Reply all
Reply to author
Forward
0 new messages