2
votes

I have a firebase cloud function trigger an send a Welcome email when someone signs up. I would like to include my email verification link in that same email to reduce the amount of emails users get upon signup and improve the onboarding experience (rather than sending two separate emails).

exports.sendWelcomeEmail = functions.auth.user().onCreate(event => {
  // Get user that signed up
  const user = event.data; // The Firebase user.

  // get the email of the user that signed up
  const email = user.email; // The email of the user.

  // Create email verification link
  var emailVerificationLink = user.createEmailVerificationLink() // NEED HELP HERE: ideally, I would like to create/call a function to create an email verification link for the user here

  // send email
  mailgun.messages().send({
    from: '[email protected]',
    to: email,
    subject: 'Welcome & Get Started',
    text: 'Welcome! Here are some resources to help you get started, but first verify your email: ' + emailVerificationLink + '!',
    html: // some nice formatted version of the text above
  }, function (error, response) {
    console.log("Email response");
    console.log(response);
    console.log("Email error");
    console.log(error);
  });

})

I have carefully looked through the documentation on custom email handlers, but it doesn't seem like they return the email verification link, so I do not see how to use that approach for my purposes here (although I hope I'm wrong).

Is there a way to create the email verification link inside a Firebase Cloud Function in such a way that I could then use resulting link as I please (like in my Welcome email)?

1

1 Answers

1
votes

There is no public API to get the OOB verification code, or the link that contains that code.

But you can implement this yourself with a few steps:

  1. Generate your own verification code, that you store somewhere securely (e.g. in a protected section of your Firebase Database).
  2. Embed that code in your message in a link.
  3. Create a Cloud Function at that link.
  4. Handle the request, check the verification code in the database
  5. Set emailVerified to true.

This isn't all that much different from what Firebase Authentication does when you call sendEmailVerification().