6
votes

I've set up Firebase email/password authentication successfully using firebase-ui.

 var uiConfig = {
        signInSuccessUrl: '<?php echo $url; ?>',
        signInOptions: [
            // Leave the lines as is for the providers you want to offer your users.
            firebase.auth.GoogleAuthProvider.PROVIDER_ID,
            firebase.auth.FacebookAuthProvider.PROVIDER_ID,
            firebase.auth.EmailAuthProvider.PROVIDER_ID
        ],
        // Terms of service url.
        tosUrl: '<your-tos-url>'
    };

    // Initialize the FirebaseUI Widget using Firebase.
    var ui = new firebaseui.auth.AuthUI(firebase.auth());
    // The start method will wait until the DOM is loaded.
    ui.start('#firebaseui-auth-container', uiConfig);

but for security reasons I want the user to confirm her/his email.But fromthe above code it doesn't send a verfication mail to user. So I've used following method to send a verification mail to user if he/she not verified his/her account mail.

firebase.auth().onAuthStateChanged(function(user) {
    if (user && user.uid != currentUid) {
        if (firebase.auth().currentUser.emailVerified) {
            currentUid = user.uid;

        else {
        //---- HERE YOU SEND THE EMAIL
            firebase.auth().currentUser.sendEmailVerification();
            }

But when I used this code it sends multiple verification mails for same account. Which means this method runs each time a user reload the page. It would be really greatful if someone could help me to identify whether verification mail sent or not for a specific user using firebase.

3
Firebase Authentication does not track whether a verification email was sent to an address already. It simply send (within reasonable limits) an email when you call sendEmailVerification(). If you want to throttle this sending, you'll have to implement a mechanism for that yourself.Frank van Puffelen
Thanks for your response Frank. Is there any option to send an email verification using firebase-ui at the moment of creating a new account??Shelly
It seems that sendEmailVerification() is only called in test code (one of the advantages of FirebaseUI being open-source is that you can search for this type of thing yourself), so it doesn't seem to be a feature. There is a feature request for it already, so you might want to chime/vote there.Frank van Puffelen
Why don't you send the email when they sign up instead? Thereafter if they aren't verified show a message that they should verify, or resend the email (as a button/link). I've not used the UI library before as using the API directly is simple but hopefully there is some callback once signed upDominic

3 Answers

2
votes

I'm late but if someone finds it like me:

if(currentUser.metadata.creationTime === currentUser.metadata.lastSignInTime)

Is true when it is a new user, so you have to send the verification email. You can also do it within signInSuccess, no need to look on onAuthStateChanged

2
votes

I found what seems to be a better answer to this barely-documented (for JavaScript) Firebase issue in the sample code here:

  // FirebaseUI config.
  var uiConfig = {
    callbacks: {
      signInSuccessWithAuthResult: function(authResult, redirectUrl) {

        var user = authResult.user;
        ...
        if (authResult.additionalUserInfo.isNewUser)
        {
            console.log("new signin");
            user.sendEmailVerification();
        }
        ...
        return true;
      },

enjoy!

0
votes

You could use SignInSuccessWithAuthResult callback to send email verification. What you need to do is provide a callback function and check if the user has verified email, if not, sendEmailVerification.