Generating email action links using the Firebase Admin SDK

Hiranya Jayathilaka
Firebase Developers
3 min readMay 5, 2019

--

Does this look familiar?

Your new account has been created.

To confirm your email and activate your account, please click the below link:

https://www.fancy.new.service/0bscureC0nf1rmat10nL1nk

If the above link does not work, please copy and paste the link on your web browser.

We’ve all received emails of this nature from various online services. It is a simple yet effective way to interact with the users, and get them to take certain actions. If you are the developer of such an online service, you might also be looking for ways to send emails when new users sign up for your service, or when existing users attempt to change their passwords.

If your application is built using Firebase, specifically Firebase Authentication, then you can easily send emails from your application prompting users to complete certain authentication-related tasks. For example, Listing 1 below shows how a web application can send out an email verification link for a newly signed up user.

Listing 1: Sending an email verification link from a Firebase app

It is worth noting that listing 1 is comprised of client-side code that runs in the user’s web browser. The sendEmailVerification() API comes from the Firebase JS SDK. This API, and others like it, make RPC calls to the Firebase Auth service, which then sends out the desired emails using an email service built into Firebase. Similar client-side APIs are available in Firebase SDKs for Android and IOS. The emails sent using these APIs contain auto-generated action links that work out of the box. What’s nice about this approach is that Firebase handles everything for you — generating email action links, placing them in a predefined email template, and sending them out.

However, there might be times when you want more control over this process. You may want to fully customize the content and layout of the emails by including your company logo and other brand information. You might even want to customize the email from user to user based on their country or language. Or you might want to use a different email service like SendGrid, where you have more visibility into the email delivery process. In these types of situations, you just need a way to obtain the email action links from Firebase. Then you can place those links in an email template of your choosing, and send them out using your preferred email service.

If this sounds like what you want, then you should look into the Firebase Admin SDK. A recent series of releases have added several new APIs to the Admin SDK that make it possible to generate Firebase email action links in server-side environments. Specifically, you will find the following new APIs in the Admin SDK:

  • generateEmailVerificationLink()
  • generatePasswordResetLink()
  • generateSignInWithEmailLink()

Listing 2 shows a serverless function that uses the Firebase Admin Node.js SDK to generate email verification links, and sends them out using a third-party library. The function itself is implemented using the Firebase Functions SDK, and it is set to execute on every new user sign up event.

Listing 2: Sending an email verification link from server-side code

The email action link APIs are now available in the Node.js, Java and Python flavors of the Firebase Admin SDK. These new APIs also allow configuring additional information that control what happens when a user clicks on a link. For example, in Listing 2 above, we indicate that the users should be redirected to the homepage of our web app once they verify their email address. Similarly you can specify whether to open the link in the web browser or in a mobile app. Check out the documentation for more examples, and see how you can incorporate these new APIs to further expand the user management capabilities of your Firebase apps.

If you wish to learn more about Firebase Admin SDKs and the new email action link APIs, check out the following presentation from the recently concluded Google Cloud Next 2019.

In this talk, Lauren Long and I walk the audience through a series of use cases that involve Firebase Auth, Cloud Firestore, Cloud Functions and Admin SDK. In the process we also demonstrate a more elaborate version of the example in listing 2 (jump to the timestamp 28:10 of the video if you’re in a hurry). Enjoy the talk, and hit me up with any questions.

--

--

Hiranya Jayathilaka
Firebase Developers

Software engineer at Shortwave. Ex-Googler. PhD in CS. Enjoys working on cloud, mobile and programming languages. Fan of all things tech and open source.