How to send email verification after user creation with Firebase Cloud functions?

Davide picture Davide · Mar 18, 2017 · Viewed 9.9k times · Source

I'm trying to send the verification email after the user is created. Since there's no way on Firebase itself, I'm trying it with cloud functions.

I cannot really find a lot of documentation about it. What I tried to do so far is:

exports.sendEmailVerification = functions.auth.user().onCreate(event => {
    return user.sendEmailVerification()
});

But I get the error that user is not defined.

How can I create this function?

Thanks!

Answer

Frank van Puffelen picture Frank van Puffelen · Mar 18, 2017

There are two possibilities to send an "email verification" email to a user:

  1. The signed-in user requests that a verification email be sent. For that, you call, from the front-end, the sendEmailVerification() method from the appropriate Client SDK.
  2. Through one of the Admin SDKs, you generate a link for email verification via the corresponding method (e.g. auth.generateEmailVerificationLink() for the Node.js Admin SDK) and you send this link via an email sent through your own mechanism. All of that is done in the back-end, and can be done in a Cloud Function.

Note that the second option with the Admin SDKs is not exactly similar to the first option with the Client SDKs: in the second option you need to send the email through your own mechanism, while in the first case, the email is automatically sent by the Firebase platform

If you'd like that ability to be added to the Admin SDK, I'd recommend you file a feature request.