2

For Web applications hosted on Firebase, I want to verify new users by emailing them a verification link. I went through the documentation, Create custom email action handlers, and configured my custom domain and have password authentication working fine.

So, how do I trigger a password verification? How do I get the oobCode and apiKey arguments? I'm registering a new user fine and nothing happens on my auth listener page?

var getArg = getArg();
var mode = getArg['mode'];
var oobCode = getArg['oobCode'];
var apiKey = getArg['apiKey'];

console.log(mode, oobCode, apiKey);

switch (mode) {
    case 'resetPassword':
        console.log('Password Request Fired');
        break;
    case 'recoverEmail':
        console.log('Recover Email Fired');
        break;
    case 'verifyEmail':
        console.log('Verify Email Fired');
        break;
}

//Thanks Geoffrey Crofte
function getArg(param) {
    var vars = {};
    window.location.href.replace(location.hash, '').replace(
        /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
        function(m, key, value) { // callback
            vars[key] = value !== undefined ? value : '';
        }
    );

    if (param) {
        return vars[param] ? vars[param] : null;
    }
    return vars;
}
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • See here, it might shed some light on what you could do: http://stackoverflow.com/documentation/proposed/changes/35793 – KhoPhi Jul 23 '16 at 22:59
  • @Rexford: please provide the necessary information as an answer. Otherwise we'll just get unanswered questions with comments linking to the docs. – Frank van Puffelen Jul 23 '16 at 23:16
  • @FrankvanPuffelen Well noted. Perhaps this could or should be marked as duplicate, because I've seen a couple of email verification related questions (this: http://stackoverflow.com/questions/17723195/is-there-any-way-to-do-email-confirmation-for-firebase-user-creation-and-or-pass/38533168#38533168 and this http://stackoverflow.com/questions/38142317/how-to-use-firebase-email-address-verification/38533115#38533115 and added the answer in the above link. – KhoPhi Jul 23 '16 at 23:21
  • Truth is a vanilla JS (no angular or jQuery) snippet would be nice. If nobody post an answer I'll come back and post one when I get it working. – Ronnie Royston Jul 23 '16 at 23:35
  • @RonRoyston In my example, I am not using AngularJS entirely. See that I do `firebase.auth().applyActionCode(...)`? That is vanilla js, because there aren't AngularJS wrappers for some, like the `.applyActionCode()` yet, at the moment. Will love to see it in vanilla js entirely. – KhoPhi Jul 23 '16 at 23:38
  • If you think the question is a duplicate, click the "close" link and vote for it as a duplicate. – Frank van Puffelen Jul 23 '16 at 23:39
  • On the SO documentation link: I'm not sure yet what the suggested process is. It sounds great to have common recipes as SO documentation, but then what to do with the questions that are answer by those docs? That might be more a question for meta though. – Frank van Puffelen Jul 23 '16 at 23:40

1 Answers1

1

firebase.auth().currentUser.sendEmailVerification() sends the mode, oobCode, and apiKey variables to the signed in users email address. Until verified, the signed in user can be identified as unverified by reading the currentUser.emailVerified property. User opens email and clicks verification link which invokes HTTP session to your auth listener page with those variables present as arguments in the URL.

Using the getArg() method in my original post (or your own) parses the arguments out of the browsers window.location (current web address/URL) into variables in your auth listener page's JavaScript.

  1. build out your auth listener page (mostly paste from docs)
  2. send signed in user an email

    firebase.auth().currentUser.sendEmailVerification()
    //checks if signed in user is verified, if not tell them to check email
    firebase.auth().currentUser.emailVerified; //false now so handle user experience appropriately until verified
    

Official Web Methods Reference Index is useful. Some other relevant methods (all thenable) are shown below:

//updates email of signed in user
-> firebase.auth().currentUser.updateEmail(newEmail)

//updates password of signed in user
-> firebase.auth().currentUser.updatePassword(newPassword)
-> firebase.auth().sendPasswordResetEmail(email)

//deletes auth account of signed in user
-> firebase.auth().currentUser.delete()
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • Thanks for posting this. There is not enough documentation on this topic. I'm having my own email verification problem. If you have any advice, please let me know. It would be very appreciated. https://stackoverflow.com/questions/51178015/firebase-email-verification-not-working-with-actioncodesetting – lucius degeer Jul 05 '18 at 19:18