Integrate Authgear with Firebase
Guide on how to use Authgear to secure Firebase services
Last updated
Was this helpful?
Guide on how to use Authgear to secure Firebase services
Last updated
Was this helpful?
Firebase is Google's flagship solution for building serverless web and mobile applications. Some of the services Firebase offers include Firestore database, Cloud Storage, Cloud Functions, and Authentication.
You can secure Firebase services like Firestore and Cloud Storage such that only authenticated users in your app have read and write access to certain parts or the entire database. This can be useful when your app allows users to generate content and you need to control who can add or view what.
In this guide, you'll learn how to use Authgear as a custom authentication system to secure Firestore database.
Authgear is a cloud-based solution that is 100% focused on user authentication and IAM. While Firebase, on the other hand, offers other cloud services such as Cloud Storage and real-time database Firestore that enable developers to build full-stack apps without the need for implementing their own backend. Hence, you can use Authgear as an authentication provider for your app that uses Firebase to enjoy all the security and login features it offers.
Custom Tokens is a feature in Firebase that allows you to authenticate users using secure JWT (JSON Web Tokens) from a third-party authentication system.
Typically, to authenticate users via Custom Tokens, your authentication system (e.g., Authgear) will authenticate users using your preferred method (e.g., username and password), then return a valid JWT to your client application.
The client application will pass the JWT to Firebase to create a Firebase user instance using the user's sub
attribute from Authgear.
The following flow diagram shows the complete sequence of using Firebase Custom Tokens and Authgear.
In this section, we'll cover a step-by-step guide for using Authgear JWT with Firebase Custom Tokens.
As part of the guide, we'll build a demo Todo app. The app will have the following features:
Users Login/Sign up
Add to-do tasks
Delete to-do tasks
Only logged-in users can add tasks
Data is stored on the cloud, and a user can only view/delete tasks they created.
In order to follow the steps in this guide, you should have the following:
Firebase project
Node.js installed on your computer
For this part, you'll login to your Firebase console and configure your project.
First, we'll create a Cloud Function that will verify the access token (JWT) from Authgear and use the uid
associated with the JWT to create a Firebase Custom Token.
Note: this may require you to upgrade your Firebase project to at least the Blaze plan.
Next, create a cloud_functions
folder in the root directory of the Authgear React example project you cloned earlier.
Change your working directory to the cloud_functions
folder:
Run the following command to install Firebase CLI:
After the CLI is installed, run the firebase login
command and follow the prompt to login to your Firebase project in the CLI.
Run the following command to initialize Cloud Function in the cloud_functions folder you created earlier:
Follow the prompt to create a v2 Cloud Function. Now you should have a function/index.js
file in your cloud_functions folder. This is where you'll write the code for your cloud function.
In this step, we'll implement a cloud function that does the following:
Accept HTTPS requests from your client application through an endpoint.
Read the value of your Authgear JWT from the HTTPS request authorization header.
Validate the JWT to ensure it is valid (not expired).
If the JWT is valid, the function will read the uid
attribute from it and use the value to call firebaseAdmin.auth().createCustomToken(uid)
. This will generate a new Firebase Custom Token that your client application can use to login to Firebase.
To implement the above function, open function/index.js
in your cloud_functions
folder and replace the content with the following:
Put your Authgear endpoint in const authgearEndpoint = "";
.
Run the following commands to install dependencies:
Finally, run firebase deploy
to deploy your new function.
On successful deploy, Firebase CLI will output the public endpoint for your function in the terminal. Copy the endpoint as you will use it later in your React app.
In this part, we'll implement the client application that will use Authgear and Firebase.
As mentioned in the prerequisites for this post, you can get the starter code for the application from our React example app repo.
Go to the Firebase Console and select your project. Next, click on Web as your target platform.
Enter the name of your app (e.g., My todo app), then click Register app.
In the Add Firebase SDK section, select Use npm, then run npm install firebase
from the root directory of the Authgear React example repo (confirm that you're not running the command in the cloud_functions folder).
Create a new firebase.js
file in the root directory of your React project.
Copy the code that includes your Firebase config, and paste it in the firebase.js
file.
Update firebase.js
to expose the Firebase services you'll be using:
Here we'll implement a to-do feature in our React app. The Todo component will contain all the code for implementing this feature.
The component will do the following:
Get the current user's access token from Authgear using the Authgear SDK.
Make an HTTPS request to your Firebase function endpoint using the fetch()
method of the Authgear SDK. The fetch()
method includes the user's access token in the HTTPS request's authorization header.
Use the custom token that is returned by the cloud function to sign in to Firebase.
Add a new todo item to the Firestore database
Display the todo items
Delete a todo item
Create a Todo.tsx
file in the src folder of your React project, then add the following code to the file:
Add your Cloud Function endpoint in const firebaseFunctionEndpoint = "";
Open App.tsx
and add the following route in <Routes>
:
Make sure to import the Todo in App.tsx.
Finally, add a router link to Home.tsx
just after the User Settings link:
You can click on the My Todos link to open the Todo page.
Return to your project in Firebase console, then navigate to Build > Firestore Database. Enable Firestore if it's not active for your project.
Go to the Rules tab, then change the security rule to the following:
The above rule will restrict read and write access to data in /todos/{userID} to only authenticated users. Click Publish to activate the new rule.
You should be able to authenticate with Authgear and add items to the to-do.
The example app used for this guide is based on the Authgear React example repo (clone or download the code ). Once you get the code, you'll have to set up an Authgear client application with type "Single Page Application" in the Authgear portal. More details are in the README.md file for the repo.
Alternatively, you can view/download the complete code with the Firebase example from a separate GitHub repo .
Authgear account (sign up for free )
To create a Cloud Function, open and navigate to Build > Functions and enable the Cloud Function if it isn't already active for your project.
Edit the IAM settings for the service account linked to your project. To do this, go to and add Service Account Token Creator
to {randomNumbers}-compute@developer.gserviceaccount.com
.
Follow the instructions in the README.md
file for the Authgear React example app to configure the app with your Authgear project. Then run the app.