2
votes

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;
}
1
See here, it might shed some light on what you could do: stackoverflow.com/documentation/proposed/changes/35793KhoPhi
@Rexford thank you!Ronnie Royston
@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
@FrankvanPuffelen Well noted. Perhaps this could or should be marked as duplicate, because I've seen a couple of email verification related questions (this: stackoverflow.com/questions/17723195/… and this stackoverflow.com/questions/38142317/… and added the answer in the above link.KhoPhi
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

1 Answers

1
votes

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