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 a user sign-in flow to your web service that uses Firebase Authentication.
In this step of the guide, you update your web service to authenticate users and to retrieve and display a user's own information after they authenticate. Note that, for this step, the site request times will still be global rather than user-specific.
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 and adding Firebase:
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-2
Add Firebase authentication methods
Firebase provides JavaScript methods and variables that you can use to configure sign-in behavior for your web service. For this web service, add a sign out function, a variable that configures the sign in UI, and a function controlling what changes when a user signs in or out.
To add the behaviors required for an authentication flow, replace your
static/script.js
file's current event listener method with the following code:
Notice that the onAuthStateChanged()
method, which controls what changes
when a user signs in or out, stores the user's ID token as a cookie.
This ID token is a unique token that Firebase generates
automatically when a user successfully signs in, and is used by the server
to authenticate the user.
Update your web service to use tokens
Next, verify users on the server using their unique Firebase ID token, then decrypt their token so that you can print their data back to them.
To use the Firebase ID token:
Retrieve, verify, and decrypt the token in the
root
method of yourmain.py
file:Ensure that your
requirements.txt
file includes all necessary dependencies:
Test your web service
Test your web service by running it locally in a virtual environment:
Run the following commands in your project's main directory to install new dependencies and run your web service. If you have not set up a virtual enviornment for local testing, see testing your web service.
pip install -r requirements.txt python main.py
Enter the following address in your web browser to view your web service:
http://localhost:8080
Deploy your web service
Now that you have authentication working locally, you can re-deploy your web service to App Engine.
Run the following command from the root directory of your project,
where your app.yaml
file is located:
gcloud app deploy
All traffic is automatically routed to the new version you deployed.
For more information on managing versions, see Managing Services and Versions.
View your service
To quickly launch your browser and access your web service at
https://PROJECT_ID.REGION_ID.r.appspot.com
, run the following
command:
gcloud app browse
Next steps
Now that you've set up user authentication, you're ready to learn how to update your web service to personalize data for authenticated users.