Shelly
Shelly

Reputation: 440

firebase-ui for email verification

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.

Upvotes: 6

Views: 3585

Answers (3)

Louis Semprini
Louis Semprini

Reputation: 3813

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!

Upvotes: 8

Ti Wang
Ti Wang

Reputation: 785

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.

Upvotes: 0

user2329440
user2329440

Reputation: 45

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

Upvotes: 3

Related Questions