Region ID
The REGION_ID
is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. For apps created after
February 2020, REGION_ID.r
is included in
App Engine URLs. For existing apps created before this date, the
region ID is optional in the URL.
Learn more about region IDs.
Add Firebase to your Google Cloud project, configure your authentication settings, and then add Firebase to your web service.
Adding Firebase to your web service enables you to authenticate users so that you can give each user a personalized experience.
Before you begin
If you have completed all the previous steps in this guide, skip this section. Otherwise, complete one of the following:
Start from Build a Python 3 App and complete all the steps leading up to this one.
If you already have a Google Cloud project, you can continue by downloading a copy of the web service:
Download the sample application repository using Git:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
Alternatively, you can download the sample as a zip file and then extract it.
Navigate to the directory that contains a copy of the files from the previous step:
cd python-docs-samples/appengine/standard_python3/building-an-app/building-an-app-1
Add Firebase to your Google Cloud project
To use Firebase authentication with your web service, add Firebase to your Google Cloud project and configure your authentication settings.
Add Firebase to your existing Google Cloud project using the Add project tool in the Firebase console.
You can also choose to use a Firebase account with a different name, not associated with your existing Google Cloud project.
Enable the authentication sign-on providers in the Firebase console. For this web service, you will enable Email/Password and Google sign-in providers:
Click Build > Authentication > Sign-in method.
Under Sign-in providers, hover the cursor over the Email/Password provider and click the pencil icon.
Toggle the Enable button to use Email/Password authentication.
After enabling the provider, click Save.
Do the same thing for the sign-in provider Google.
For Firebase to authenticate properly, your domain needs to be authorized for OAuth redirects. To authorize your domain:
Select Build > Authentication > Settings.
Under Authorized domains on the Settings page, click Add Domain.
Enter the domain of your app on App Engine, excluding the
http://
prefix:PROJECT_ID
.REGION_ID.r.appspot.com
wherePROJECT_ID
is the ID of your Google Cloud project.
Add Firebase to your web service
To add Firebase to your web service, copy your Firebase project's custom code snippet, JavaScript and CSS files into your web service:
Go to the Firebase console and select your project.
From the project overview page, under the text Get started by adding Firebase to your app, select web. If you already have an app added to the project, you may not see this text; instead, navigate to the Project Overview > Project settings > General page of your existing app, scroll down, and select Add app.
Once the app is registered, a customized code snippet will be displayed. Copy the contents of the snippet. To see this code snippet again later, navigate to the Project settings page for your Firebase app.
Update your
templates/index.html
file by completing the following:Add the following lines to the
<head>
tag:Add your customized code snippet to the
<body>
tag.For this tutorial, you can add the code to the top of the body, since the only content in
templates/index.html
is an example of Firebase services. In your production environment, we recommend that you add the code snippet to the bottom of the body, but before you use any Firebase services.Your custom code will look similar to this mock snippet:
<!-- MOCK SNIPPET: DO NOT COPY --> <!-- The core Firebase JS SDK is always required and must be listed first --> <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-app.js"></script> <!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries --> <script> var config = { apiKey: "<API_KEY>", authDomain: "<PROJECT_ID>.firebaseapp.com", databaseURL: "https://<DATABASE_NAME>.firebaseio.com", projectId: "<PROJECT_ID>", storageBucket: "<BUCKET>.appspot.com", messagingSenderId: "<SENDER_ID>", }; firebase.initializeApp(config); </script>
Replace the TODO in the snippet above with the following script tag to enable the Authentication component of Firebase:
<script src="https://www.gstatic.com/firebasejs/7.8.0/firebase-auth.js"></script>
These script addresses are documented in the Firebase UI for Web documentation.
Replace the rest of the body with the following code, which you will use later in this guide to display authenticated user data:
Next Steps
Now that you've added Firebase to your Google Cloud project and your web service, you're ready to add code to your web service to enable it to authenticate users.