Google Firebase offers a number of products that help speed up the development of both mobile and web applications. The cornerstone product is their real-time database which offers a No-SQL style JSON store. It’s pretty magical to work with. Combine it with their free authentication service, and you can build entire applications with just front-end code (HTML, CSS, JS).

Google Cloud Functions takes all of this a step further by providing a way to queue up code to run behind the scenes, completely removing the need for a backend server. Functions are written in Node so that you can import any existing node modules out of the box.

Code inside a Cloud Function just sits there until it’s triggered. You can trigger Cloud Functions a few different ways:

Realtime Database Triggers - by adding/editing/removing items from the DB

The code you write inside a Cloud Function has admin access to everything within your Firebase account. Based on a trigger you can do lots of things, like get/set data from the database, or update a file in cloud storage. With HTTP triggers, once the function runs, you can even return data to the browser client.

For this tutorial, we’ll set up a simple Firebase database via JavaScript and use the real-time database trigger to fire off a transactional email via Postmark.

I’ve included jQuery here to simplify things, but it’s by no means mandatory.

You’ll want to update the code in this section with the code that you received after setting up your Firebase project.

The authFirebaseInit() function does the heavy lifting here. Upon success, it redirects the user to /admin. Failures are logged in the console. If this is for a real-world project, you could add some error handling within the error catch section.

In a browser, you should see:

Once you click the blue sign in button, you’ll be prompted with the Google OAuth login popup.

Nothing needs to change in this file. Props to David King for coming up with this approach. It uses globbing to search the functions folder for files ending with .f.js and auto-includes them keeping everything nice and tidy.