From 59910d70ffc84f7d0d89788edc6e70d18c6a7f60 Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Thu, 3 Sep 2020 01:16:29 +0000 Subject: [PATCH 01/15] begin tutorial outline --- .../index.md | 56 +++++++++++++++++++ .../notes.txt | 19 +++++++ 2 files changed, 75 insertions(+) create mode 100644 tutorials/using-flask-login-with-cloud-datastore/index.md create mode 100644 tutorials/using-flask-login-with-cloud-datastore/notes.txt diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md new file mode 100644 index 0000000000..02725baac7 --- /dev/null +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -0,0 +1,56 @@ +--- +title: Using Flask-Login with Cloud Firestore in Datastore mode +description: Represent your Cloud Datastore entity with a Python class(ie a model) and use this for Flask-Login user management +author: komlasapaty +tags: Flask Framework, Python 3, Firestore in Datastore mode +date_published: +--- + +In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. + +User authentication and user session management is a crucial component of most web applications. +Flask-Login is a Flask extension that helps the developer to authenticate and manage user session. +Flask-Login requires the user to be represented using a Python class with specific properties and methods provided. + +The above requirement of Flask-Login is straightforward when using a relational database like MySQL or Postgres. +Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. + +However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. + +This tutorial will demonstrate how to use Flask-Login with Firestore in Datastore mode with the help of a little library; Datastore-Entity. + +_Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ + +Datastore-Entity library lets you map your datastore entity using Python classes. Think of it as an ORM-like library for Firestore in Datastore mode. + +# Requirements + +- [Python3.7](https://www.python.org/downloads/) +- [Flask](https://github.com/pallets/flask) +- [Flask-Login](https://flask-login.readthedocs.io) +- [Datastore-Entity](https://datastore-entity.readthedocs.io) + + +##Prerequisites +Familiarity with Flask and Flask-Login +Familiarity with App Engine and Firestore in Datastore mode. +An active App Engine application (Projects that use the Datastore mode API require an active App Engine application.) + + +##Step to +Install the libraries +Map your user class(using UserMixin) +Implement /login view endpoint + + +#Next Steps +The same approach can be used with populare libraries like WTForms etc. + + +# Useful links +- [Firestore in Datastore mode Documentation](https://cloud.google.com/datastore) +- [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/) +- [Flask-Login Documentation](https://flask-login.readthedocs.io) +- [Datastore-Entity Documentation](https://datastore-entity.readthedocs.io) +- [Datastore-Entity on Github](https://github.com/komlasapaty/datastore-entity) +- [Flask-Login Tutorial Using Postgres](https://hackersandslackers.com/flask-login-user-authentication) \ No newline at end of file diff --git a/tutorials/using-flask-login-with-cloud-datastore/notes.txt b/tutorials/using-flask-login-with-cloud-datastore/notes.txt new file mode 100644 index 0000000000..47f9686ddc --- /dev/null +++ b/tutorials/using-flask-login-with-cloud-datastore/notes.txt @@ -0,0 +1,19 @@ +https://cloud.google.com/community/tutorials/building-flask-api-with-cloud-firestore-and-deploying-to-cloud-run + +Using Flask-Login Extension With Cloud Datastore +Prerequisites: + Familiarity with Flask (link to flask tutorial) + Familiarity with Cloud Datastore (link to datastore tutorial) + Familiarity with Flask-Login Extension (link to flask login tutorial) + +Datastore-Entity Tutorial with Flask Login +Flask Login requires a User class +Most Flask usage requires the use of models(with sql-alchemy) +Datastore Entity library helps you represent your datastore entity using Python classes(just like a relational database model) + +To easily represent your user entity as a class, we'll use a +library called datastore-entity(disclaimer: I authored this library. No Google affiliation) + +pip install flask-login +pip install datastore-entity + From 52793b9283fba385dd786d5657169f5d5c910b39 Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Thu, 3 Sep 2020 01:30:04 +0000 Subject: [PATCH 02/15] add new lines and format --- .../index.md | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 02725baac7..2173fe4c17 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -8,20 +8,20 @@ date_published: In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. -User authentication and user session management is a crucial component of most web applications. -Flask-Login is a Flask extension that helps the developer to authenticate and manage user session. -Flask-Login requires the user to be represented using a Python class with specific properties and methods provided. +User authentication and user session management is a crucial component of most web applications. +Flask-Login is a Flask extension that helps the developer to authenticate and manage user session. +Flask-Login requires the user to be represented using a Python class with specific properties and methods provided. The above requirement of Flask-Login is straightforward when using a relational database like MySQL or Postgres. -Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. +Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. -However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. +However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. -This tutorial will demonstrate how to use Flask-Login with Firestore in Datastore mode with the help of a little library; Datastore-Entity. +This tutorial will demonstrate how to use Flask-Login with Firestore in Datastore mode with the help of a little library; Datastore-Entity. -_Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ +_Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ -Datastore-Entity library lets you map your datastore entity using Python classes. Think of it as an ORM-like library for Firestore in Datastore mode. +Datastore-Entity library lets you map your datastore entity using Python classes. Think of it as an ORM-like library for Firestore in Datastore mode. # Requirements @@ -32,12 +32,14 @@ Datastore-Entity library lets you map your datastore entity using Python classes ##Prerequisites + Familiarity with Flask and Flask-Login Familiarity with App Engine and Firestore in Datastore mode. An active App Engine application (Projects that use the Datastore mode API require an active App Engine application.) -##Step to +##Step to + Install the libraries Map your user class(using UserMixin) Implement /login view endpoint From e615b9a74735846b2611112374c2a9c4db589a3c Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Thu, 3 Sep 2020 21:29:33 +0000 Subject: [PATCH 03/15] add sample code --- .../index.md | 75 ++++++++++++++++--- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 2173fe4c17..60c69b3638 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -6,13 +6,13 @@ tags: Flask Framework, Python 3, Firestore in Datastore mode date_published: --- -In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. +In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend/backing store. User authentication and user session management is a crucial component of most web applications. -Flask-Login is a Flask extension that helps the developer to authenticate and manage user session. +Flask-Login is a Flask extension that helps the developer to hanle authentication and user session management. Flask-Login requires the user to be represented using a Python class with specific properties and methods provided. -The above requirement of Flask-Login is straightforward when using a relational database like MySQL or Postgres. +The above requirement of Flask-Login is straightforward when using a relational databases like MySQL or Postgres. Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. @@ -31,25 +31,82 @@ Datastore-Entity library lets you map your datastore entity using Python classes - [Datastore-Entity](https://datastore-entity.readthedocs.io) -##Prerequisites +## Prerequisites Familiarity with Flask and Flask-Login Familiarity with App Engine and Firestore in Datastore mode. An active App Engine application (Projects that use the Datastore mode API require an active App Engine application.) -##Step to +## Step to -Install the libraries +Install required libraries Map your user class(using UserMixin) Implement /login view endpoint -#Next Steps -The same approach can be used with populare libraries like WTForms etc. +```python +from flask_login import UserMixin +from datastore_entity import DatastoreEntity, EntityValue +import datetime -# Useful links + +class User(DatastoreEntity, UserMixin): + username = EntityValue(None) + password = EntityValue(None) + status = EntityValue(1) + date_created = EntityValue(datetime.datetime.utcnow()) + + +``` + + +Your flask login and logout views +```python +from flask_login import login_required, logout_user, login_user + +@page.route("/login", methods=['GET','POST']) +def login(): + form = LoginForm(next=request.args.get('next')) + + if form.validate_on_submit(): + identity = request.form.get('username') + password = request.form.get('password') + user = User().get_object(identity) #fetch user using the username + + if user and user.authenticated(password): # authenticate user here + + if login_user(user, remember=True): + + #handle optionally redirecting to the next URL safely + next_url = form.next.data + if next_url: + return redirect(safe_next_url(next_url)) + + return redirect(url_for('page.dashboard')) + else: + flash('This account is not active','error') + + else: #authentication failed + flash('Login or password is incorrect','error') + + return render_template("page/login.html", form=form) + + +@page.route("/logout") +@login_required +def logout(): + logout_user() + flash("You have successfully logged out", "success") + return redirect(url_for('page.login')) +``` + +## Next Steps +The same approach can be used with popular libraries like WTForms where a database model is populated with the form values using ```form.populate_obj(model) + + +## Useful links - [Firestore in Datastore mode Documentation](https://cloud.google.com/datastore) - [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/) - [Flask-Login Documentation](https://flask-login.readthedocs.io) From 6d75ed1ba6d09643a1215e3c7f58fb3469bc0904 Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Fri, 4 Sep 2020 15:51:22 +0000 Subject: [PATCH 04/15] add setting up of local environment --- .../index.md | 70 ++++++++++++++++--- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 60c69b3638..4ba8610648 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -9,8 +9,8 @@ date_published: In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend/backing store. User authentication and user session management is a crucial component of most web applications. -Flask-Login is a Flask extension that helps the developer to hanle authentication and user session management. -Flask-Login requires the user to be represented using a Python class with specific properties and methods provided. +Flask-Login is a Flask extension that helps the developer to handle authentication and user session management. +Flask-Login requires the user(ie application user) to be represented using a Python class with specific properties and methods provided. The above requirement of Flask-Login is straightforward when using a relational databases like MySQL or Postgres. Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. @@ -18,11 +18,11 @@ Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. This tutorial will demonstrate how to use Flask-Login with Firestore in Datastore mode with the help of a little library; Datastore-Entity. +This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class in order to conveniently use popular Python libraries like Flask-Login, WTForms etc +To represent our Datastore entity with a Python class, we will use a library called Datastore-Entity. Think of Datastore-Entity as an ORM-like library for Firestore in Datastore mode. _Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ -Datastore-Entity library lets you map your datastore entity using Python classes. Think of it as an ORM-like library for Firestore in Datastore mode. - # Requirements - [Python3.7](https://www.python.org/downloads/) @@ -32,18 +32,56 @@ Datastore-Entity library lets you map your datastore entity using Python classes ## Prerequisites +- Basic familiarity with Flask and Flask-Login. +- Basic familiarity with App Engine and Firestore in Datastore mode. -Familiarity with Flask and Flask-Login -Familiarity with App Engine and Firestore in Datastore mode. -An active App Engine application (Projects that use the Datastore mode API require an active App Engine application.) +This short tutorial does not show a full Flask application or the basic use of Firestore in Datastore mode. +It is assumed the reader has basic familiarity with these. -## Step to +## Step to +Setting up your local environment Install required libraries Map your user class(using UserMixin) -Implement /login view endpoint +Implement Flask /login view endpoint + + +### Set Up Your Local Environment +Note: Projects that use the Datastore mode API require an active App Engine application. +So you should already have App Engine and Datastore mode API enabled in your GCP project. + +As usual, to connect to your Google Cloud service from your local machine you need the appropriate credentials(service account key). +_Create and download service account key_ +- On the GCP console, go to **Main menu** -> **IAM & Admin** -> click **Service Account** on the left pane -> click **CREATE SERVICE ACCOUNT** at the top. +- Give the service account an appropriate name (eg. datastore-service-account). Enter an optional description below and click **CREATE** +- Under **Select a role** dropdown menu, type **Cloud Datastore owner** in the filter field, select it and click **CONTINUE** and click **DONE** on the next page. +- On the **Service Account** page, look for the service account you just created and click the overflow icon (vertical ellipsis) at the far right end. +- Click **Create Key** from the dropdown menu and make sure **JSON** is selected as the **Key type**. +- Click **CREATE**. This automatically downloads the service account JSON key to your local machine. Take note of the file name on the popup screen. + + +_Configure your service account on your local machine_ +Point the environment variable **GOOGLE_APPLICATION_CREDENTIALS** to the location of the service account key you downloaded. +**Linux/maxOS** +export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" +**Windows** +_With Powershell_ +$env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" +_With Command Prompt_ +set GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" + +You are now ready to connect to your Firestore in Datastore mode. + + +### Install required libraries +```bash +pip install flask flask-login datastore-entity +``` +**NOTE:** Installing datastore-entity installs the required client libraries for **Firestore in Datastore mode** + +### Create your Flask user model ```python @@ -62,6 +100,20 @@ class User(DatastoreEntity, UserMixin): ``` +```python +from flask import Flask + +app = Flask(__main__) +app.secret_key = "development_key" #not secure + + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') +``` + + + + Your flask login and logout views ```python from flask_login import login_required, logout_user, login_user From 2bb748863006010e37b203a72b35f2e85886835e Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Sat, 5 Sep 2020 14:07:45 +0000 Subject: [PATCH 05/15] add tutorial prerequisite --- .../index.md | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 4ba8610648..282f395447 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -6,19 +6,19 @@ tags: Flask Framework, Python 3, Firestore in Datastore mode date_published: --- -In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend/backing store. +In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. User authentication and user session management is a crucial component of most web applications. Flask-Login is a Flask extension that helps the developer to handle authentication and user session management. -Flask-Login requires the user(ie application user) to be represented using a Python class with specific properties and methods provided. +Flask-Login **requires** the user(ie application user) to be represented using a Python class with specific properties and methods provided. -The above requirement of Flask-Login is straightforward when using a relational databases like MySQL or Postgres. +The above requirement of Flask-Login is straightforward when using a relational database like MySQL or Postgres. Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. This tutorial will demonstrate how to use Flask-Login with Firestore in Datastore mode with the help of a little library; Datastore-Entity. -This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class in order to conveniently use popular Python libraries like Flask-Login, WTForms etc +This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class in order to conveniently use popular Python libraries like Flask-Login, WTForms etc. To represent our Datastore entity with a Python class, we will use a library called Datastore-Entity. Think of Datastore-Entity as an ORM-like library for Firestore in Datastore mode. _Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ @@ -32,6 +32,10 @@ _Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ ## Prerequisites +This tutorial is not to teach the fundamentals of Flask-Login or Firestore in Datastore mode. It is to demonstrate the use of Firestore in Datastore mode(as opposed to any relational database) as the database backend for user authentication with Flask-Login. +The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode. + +So familiarity with the following is assumed: - Basic familiarity with Flask and Flask-Login. - Basic familiarity with App Engine and Firestore in Datastore mode. @@ -82,7 +86,7 @@ pip install flask flask-login datastore-entity ### Create your Flask user model - +Model your ```user``` entity. ```python from flask_login import UserMixin @@ -96,6 +100,10 @@ class User(DatastoreEntity, UserMixin): status = EntityValue(1) date_created = EntityValue(datetime.datetime.utcnow()) + # other fields or method go here... + #def authenticated(self, password): + # authenticate + ``` @@ -112,8 +120,6 @@ if __name__ == '__main__': ``` - - Your flask login and logout views ```python from flask_login import login_required, logout_user, login_user From 2a37c369ac190d98e45c5df4ca2a5f38c0b5600a Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Sun, 6 Sep 2020 23:12:38 +0000 Subject: [PATCH 06/15] improve formatting, shorten sentences --- .../index.md | 98 +++++++------------ 1 file changed, 37 insertions(+), 61 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 282f395447..bac30ef6f4 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -8,51 +8,44 @@ date_published: In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. -User authentication and user session management is a crucial component of most web applications. + +## Prerequisites +This tutorial is not to teach the fundamentals of Flask-Login or Firestore in Datastore mode. It is to demonstrate the use of Firestore in Datastore mode(as opposed to any relational database) as the database backend for user authentication with Flask-Login. +The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode. + +Familiarity with the following is assumed: +- Flask and Flask-Login. +- App Engine and Firestore in Datastore mode. + +## Introduction Flask-Login is a Flask extension that helps the developer to handle authentication and user session management. Flask-Login **requires** the user(ie application user) to be represented using a Python class with specific properties and methods provided. -The above requirement of Flask-Login is straightforward when using a relational database like MySQL or Postgres. -Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database and then add the methods and properties required by Flask-Login. - -However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge since the use of models/classes do not directly apply. +The above requirement of Flask-Login is straightforward when using a relational database such as MySQL or Postgres. +Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database. +Methods and properties required by Flask-Login can then be added to the model class. -This tutorial will demonstrate how to use Flask-Login with Firestore in Datastore mode with the help of a little library; Datastore-Entity. -This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class in order to conveniently use popular Python libraries like Flask-Login, WTForms etc. +However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge. +This is because the use of a model/class does not directly apply. + +This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class. +This will let you conveniently use popular Python libraries like **Flask-Login**, **WTForms** etc. +These libraries often use patterns that rely on a database record being represented as a Python class. -To represent our Datastore entity with a Python class, we will use a library called Datastore-Entity. Think of Datastore-Entity as an ORM-like library for Firestore in Datastore mode. -_Disclaimer: I'm the author of datastore-entity library. No Google affiliation._ +To model our Datastore entity, we will use **Datastore-Entity** library. +Think of Datastore-Entity as an _ORM-like_ library for Firestore in Datastore mode. +_Disclaimer: I'm the author of datastore-entity. No Google affiliation._ -# Requirements +## Requirements - [Python3.7](https://www.python.org/downloads/) - [Flask](https://github.com/pallets/flask) - [Flask-Login](https://flask-login.readthedocs.io) -- [Datastore-Entity](https://datastore-entity.readthedocs.io) - - -## Prerequisites -This tutorial is not to teach the fundamentals of Flask-Login or Firestore in Datastore mode. It is to demonstrate the use of Firestore in Datastore mode(as opposed to any relational database) as the database backend for user authentication with Flask-Login. -The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode. - -So familiarity with the following is assumed: -- Basic familiarity with Flask and Flask-Login. -- Basic familiarity with App Engine and Firestore in Datastore mode. - -This short tutorial does not show a full Flask application or the basic use of Firestore in Datastore mode. -It is assumed the reader has basic familiarity with these. - - - -## Step to -Setting up your local environment -Install required libraries -Map your user class(using UserMixin) -Implement Flask /login view endpoint +- [Datastore-Entity](https://datastore-entity.readthedocs.io) ### Set Up Your Local Environment -Note: Projects that use the Datastore mode API require an active App Engine application. +NOTE: Projects that use the Datastore mode API require an active App Engine application. So you should already have App Engine and Datastore mode API enabled in your GCP project. As usual, to connect to your Google Cloud service from your local machine you need the appropriate credentials(service account key). @@ -82,7 +75,7 @@ You are now ready to connect to your Firestore in Datastore mode. ```bash pip install flask flask-login datastore-entity ``` -**NOTE:** Installing datastore-entity installs the required client libraries for **Firestore in Datastore mode** +NOTE: Installing datastore-entity installs the required client libraries for **Firestore in Datastore mode.** ### Create your Flask user model @@ -102,25 +95,14 @@ class User(DatastoreEntity, UserMixin): # other fields or method go here... #def authenticated(self, password): - # authenticate + # ... ``` - -```python -from flask import Flask - -app = Flask(__main__) -app.secret_key = "development_key" #not secure - - -if __name__ == '__main__': - app.run(debug=True, host='0.0.0.0') -``` - - -Your flask login and logout views +### Fetch user with your model +Once the entity is modelled, you can fetch your user entity using familiar ORM pattern. +An example flask login view. ```python from flask_login import login_required, logout_user, login_user @@ -131,9 +113,11 @@ def login(): if form.validate_on_submit(): identity = request.form.get('username') password = request.form.get('password') - user = User().get_object(identity) #fetch user using the username - if user and user.authenticated(password): # authenticate user here + # fetch user using the 'username' property + user = User().get_object('username',identity) + + if user and user.authenticated(password): if login_user(user, remember=True): @@ -142,26 +126,18 @@ def login(): if next_url: return redirect(safe_next_url(next_url)) - return redirect(url_for('page.dashboard')) + return redirect(url_for('page/dashboard.html')) else: flash('This account is not active','error') - else: #authentication failed + else: flash('Login or password is incorrect','error') return render_template("page/login.html", form=form) - -@page.route("/logout") -@login_required -def logout(): - logout_user() - flash("You have successfully logged out", "success") - return redirect(url_for('page.login')) ``` -## Next Steps -The same approach can be used with popular libraries like WTForms where a database model is populated with the form values using ```form.populate_obj(model) +The same approach can be used to persist/update entity data using the entity model. ## Useful links From 02519bdc8c080b954fe28d40f9086f4d00234677 Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Sun, 6 Sep 2020 23:20:11 +0000 Subject: [PATCH 07/15] format code --- .../index.md | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index bac30ef6f4..a346569084 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -29,7 +29,7 @@ However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login This is because the use of a model/class does not directly apply. This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class. -This will let you conveniently use popular Python libraries like **Flask-Login**, **WTForms** etc. +This will let you conveniently use popular Python libraries like **Flask-Login**, **WTForms** etc. These libraries often use patterns that rely on a database record being represented as a Python class. To model our Datastore entity, we will use **Datastore-Entity** library. @@ -58,16 +58,21 @@ _Create and download service account key_ - Click **CREATE**. This automatically downloads the service account JSON key to your local machine. Take note of the file name on the popup screen. -_Configure your service account on your local machine_ -Point the environment variable **GOOGLE_APPLICATION_CREDENTIALS** to the location of the service account key you downloaded. +_Configure your service account on your local machine_ +Point the environment variable **GOOGLE_APPLICATION_CREDENTIALS** to the location of the service account key you downloaded. **Linux/maxOS** +```bash export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" -**Windows** -_With Powershell_ +``` +**Windows** +_With Powershell_ +``` $env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" -_With Command Prompt_ +``` +_With Command Prompt_ +``` set GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" - +``` You are now ready to connect to your Firestore in Datastore mode. @@ -93,11 +98,10 @@ class User(DatastoreEntity, UserMixin): status = EntityValue(1) date_created = EntityValue(datetime.datetime.utcnow()) - # other fields or method go here... + # other fields or methods go here... #def authenticated(self, password): # ... - ``` ### Fetch user with your model @@ -120,6 +124,7 @@ def login(): if user and user.authenticated(password): if login_user(user, remember=True): + user.update_activity() #handle optionally redirecting to the next URL safely next_url = form.next.data From e5d0c93f927148bfbf85b385c1f81ab58f8ebcf1 Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Sun, 6 Sep 2020 23:34:20 +0000 Subject: [PATCH 08/15] datastore as drop-in replacement --- tutorials/using-flask-login-with-cloud-datastore/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index a346569084..f336515db9 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -142,7 +142,8 @@ def login(): ``` -The same approach can be used to persist/update entity data using the entity model. +The same approach can be used to persist/update entity data using the entity class/model. +This technique can let Firestore in Datastore mode be a drop-in replacement for your relational database with little to no code changes. ## Useful links From f31a90e579c955221187c3b46c8e92b0cecf5bbe Mon Sep 17 00:00:00 2001 From: Seyram Komla Date: Mon, 7 Sep 2020 23:31:27 +0000 Subject: [PATCH 09/15] add a 'conclusion' section --- .../index.md | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index f336515db9..ae904835f6 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -1,50 +1,51 @@ --- title: Using Flask-Login with Cloud Firestore in Datastore mode -description: Represent your Cloud Datastore entity with a Python class(ie a model) and use this for Flask-Login user management +description: Represent your Cloud Datastore entity with a Python class and use this for Flask-Login user management author: komlasapaty tags: Flask Framework, Python 3, Firestore in Datastore mode date_published: --- -In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) and [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. +In this tutorial, you will implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) with [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. ## Prerequisites -This tutorial is not to teach the fundamentals of Flask-Login or Firestore in Datastore mode. It is to demonstrate the use of Firestore in Datastore mode(as opposed to any relational database) as the database backend for user authentication with Flask-Login. -The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode. +This tutorial is not to teach the fundamentals of Flask-Login or Firestore in Datastore mode. It is to demonstrate the use of Firestore in Datastore mode(as opposed to any relational database) as the database backend for user authentication with Flask-Login. +The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode in favour of a relational database. -Familiarity with the following is assumed: +Basic familiarity with the following is assumed: - Flask and Flask-Login. - App Engine and Firestore in Datastore mode. +## Requirements + +- [Python3.7](https://www.python.org/downloads/) +- [Flask](https://github.com/pallets/flask) +- [Flask-Login](https://flask-login.readthedocs.io) +- [Datastore-Entity](https://datastore-entity.readthedocs.io) + + ## Introduction -Flask-Login is a Flask extension that helps the developer to handle authentication and user session management. Flask-Login **requires** the user(ie application user) to be represented using a Python class with specific properties and methods provided. -The above requirement of Flask-Login is straightforward when using a relational database such as MySQL or Postgres. +The above requirement of Flask-Login is straightforward when using a relational database such as MySQL or Postgres. Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database. -Methods and properties required by Flask-Login can then be added to the model class. +Methods and properties required by Flask-Login can then be added to the user model. + +However, for a NoSQL database like Firestore in Datastore mode, this Flask-Login requirement poses a challenge. +This is because the pattern of using a model/class to represent a database record does not directly apply. + + -However, for a NoSQL database like Firestore in Datastore mode, the Flask-Login requirement poses a challenge. -This is because the use of a model/class does not directly apply. - This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class. This will let you conveniently use popular Python libraries like **Flask-Login**, **WTForms** etc. -These libraries often use patterns that rely on a database record being represented as a Python class. -To model our Datastore entity, we will use **Datastore-Entity** library. +To model our Datastore entity as a Python class, we will use **Datastore-Entity** library. Think of Datastore-Entity as an _ORM-like_ library for Firestore in Datastore mode. _Disclaimer: I'm the author of datastore-entity. No Google affiliation._ -## Requirements - -- [Python3.7](https://www.python.org/downloads/) -- [Flask](https://github.com/pallets/flask) -- [Flask-Login](https://flask-login.readthedocs.io) -- [Datastore-Entity](https://datastore-entity.readthedocs.io) - -### Set Up Your Local Environment +### Set up your local environment NOTE: Projects that use the Datastore mode API require an active App Engine application. So you should already have App Engine and Datastore mode API enabled in your GCP project. @@ -80,8 +81,7 @@ You are now ready to connect to your Firestore in Datastore mode. ```bash pip install flask flask-login datastore-entity ``` -NOTE: Installing datastore-entity installs the required client libraries for **Firestore in Datastore mode.** - +NOTE: Installing datastore-entity installs the required/official client library for **Firestore in Datastore mode.** ### Create your Flask user model Model your ```user``` entity. @@ -100,15 +100,18 @@ class User(DatastoreEntity, UserMixin): # other fields or methods go here... #def authenticated(self, password): - # ... + # ... ``` -### Fetch user with your model -Once the entity is modelled, you can fetch your user entity using familiar ORM pattern. -An example flask login view. +### Implement the Flask 'login' view +Once the entity is modelled, you can fetch your user entity using familiar ORM pattern. +Use the methods provided on your model to fetch the user record. + +An example flask login view. ```python -from flask_login import login_required, logout_user, login_user +from flask_login import login_user +from models import User @page.route("/login", methods=['GET','POST']) def login(): @@ -119,6 +122,7 @@ def login(): password = request.form.get('password') # fetch user using the 'username' property + # refer to the datastore-entity documentation for more user = User().get_object('username',identity) if user and user.authenticated(password): @@ -141,8 +145,8 @@ def login(): return render_template("page/login.html", form=form) ``` - -The same approach can be used to persist/update entity data using the entity class/model. +### Conclusion +You can employ same approach to model any other Firestore in Datastore mode entity to persist/update data. This technique can let Firestore in Datastore mode be a drop-in replacement for your relational database with little to no code changes. @@ -151,5 +155,4 @@ This technique can let Firestore in Datastore mode be a drop-in replacement for - [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/) - [Flask-Login Documentation](https://flask-login.readthedocs.io) - [Datastore-Entity Documentation](https://datastore-entity.readthedocs.io) -- [Datastore-Entity on Github](https://github.com/komlasapaty/datastore-entity) - [Flask-Login Tutorial Using Postgres](https://hackersandslackers.com/flask-login-user-authentication) \ No newline at end of file From d498f967152de4043e296f02e3f05c0e24f5624a Mon Sep 17 00:00:00 2001 From: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com> Date: Sat, 12 Sep 2020 15:32:42 -0700 Subject: [PATCH 10/15] fixing frontmatter --- tutorials/using-flask-login-with-cloud-datastore/index.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index ae904835f6..a59ae2c05f 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -1,9 +1,9 @@ --- title: Using Flask-Login with Cloud Firestore in Datastore mode -description: Represent your Cloud Datastore entity with a Python class and use this for Flask-Login user management +description: Represent your Cloud Datastore entity with a Python class and use this for Flask-Login user management. author: komlasapaty -tags: Flask Framework, Python 3, Firestore in Datastore mode -date_published: +tags: Flask Framework, Python 3 +date_published: 2020-9-15 --- In this tutorial, you will implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) with [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. @@ -155,4 +155,4 @@ This technique can let Firestore in Datastore mode be a drop-in replacement for - [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/) - [Flask-Login Documentation](https://flask-login.readthedocs.io) - [Datastore-Entity Documentation](https://datastore-entity.readthedocs.io) -- [Flask-Login Tutorial Using Postgres](https://hackersandslackers.com/flask-login-user-authentication) \ No newline at end of file +- [Flask-Login Tutorial Using Postgres](https://hackersandslackers.com/flask-login-user-authentication) From f21ccdf182845f6469430807987afec20453a68c Mon Sep 17 00:00:00 2001 From: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com> Date: Sat, 12 Sep 2020 15:34:56 -0700 Subject: [PATCH 11/15] Update index.md --- tutorials/using-flask-login-with-cloud-datastore/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index a59ae2c05f..49f8bd66c2 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -3,7 +3,7 @@ title: Using Flask-Login with Cloud Firestore in Datastore mode description: Represent your Cloud Datastore entity with a Python class and use this for Flask-Login user management. author: komlasapaty tags: Flask Framework, Python 3 -date_published: 2020-9-15 +date_published: 2020-09-15 --- In this tutorial, you will implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) with [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. From fa3060edcea27afd1f518f223e511a10a85c9c5b Mon Sep 17 00:00:00 2001 From: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com> Date: Thu, 24 Sep 2020 14:42:27 -0700 Subject: [PATCH 12/15] line edit through introduction --- .../index.md | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 49f8bd66c2..6436e7aaad 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -3,49 +3,51 @@ title: Using Flask-Login with Cloud Firestore in Datastore mode description: Represent your Cloud Datastore entity with a Python class and use this for Flask-Login user management. author: komlasapaty tags: Flask Framework, Python 3 -date_published: 2020-09-15 +date_published: 2020-09-25 --- -In this tutorial, you will implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) with [**Firestore in Datastore mode**](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. +In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) with +[Firestore in Datastore mode](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. +This tutorial is not meant to teach the fundamentals of Flask-Login or Firestore in Datastore mode. This tutorial demonstrates the use of Firestore in Datastore +mode (as opposed to any relational database) as the database backend for user authentication with Flask-Login. -## Prerequisites -This tutorial is not to teach the fundamentals of Flask-Login or Firestore in Datastore mode. It is to demonstrate the use of Firestore in Datastore mode(as opposed to any relational database) as the database backend for user authentication with Flask-Login. -The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode in favour of a relational database. +The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode in favor of a relational database. Basic familiarity with the following is assumed: -- Flask and Flask-Login. -- App Engine and Firestore in Datastore mode. + +- Flask +- Flask-Login +- App Engine +- Firestore in Datastore mode ## Requirements - [Python3.7](https://www.python.org/downloads/) - [Flask](https://github.com/pallets/flask) - [Flask-Login](https://flask-login.readthedocs.io) -- [Datastore-Entity](https://datastore-entity.readthedocs.io) - +- [Datastore Entity](https://datastore-entity.readthedocs.io) ## Introduction -Flask-Login **requires** the user(ie application user) to be represented using a Python class with specific properties and methods provided. -The above requirement of Flask-Login is straightforward when using a relational database such as MySQL or Postgres. -Using an ORM toolkit like SQL-Alchemy, you can easily create a user model/class to represent a user in a relational database. -Methods and properties required by Flask-Login can then be added to the user model. +Flask-Login requires the user (that is, the application user) to be represented using a Python class with specific properties and methods provided. This +requirement of Flask-Login is straightforward when using a relational database such as MySQL or Postgres. Using an ORM toolkit like SQL-Alchemy, you can easily +create a user model/class to represent a user in a relational database. Methods and properties required by Flask-Login can then be added to the user model. -However, for a NoSQL database like Firestore in Datastore mode, this Flask-Login requirement poses a challenge. -This is because the pattern of using a model/class to represent a database record does not directly apply. +However, for a NoSQL database like Firestore in Datastore mode, this Flask-Login requirement poses a challenge, because the pattern of using a model/class to +represent a database record does not directly apply. +This tutorial demonstrates how to model your Firestore in Datastore mode entity as a Python class, which lets you use popular Python libraries like +Flask-Login and WTForms. +To model a Datastore entity as a Python class, this tutorial uses the [Datastore Entity library](https://datastore-entity.readthedocs.io). Think of Datastore +Entity as an ORM-like library for Firestore in Datastore mode. -This tutorial will demonstrate how to model your Firestore in Datastore mode entity as a Python class. -This will let you conveniently use popular Python libraries like **Flask-Login**, **WTForms** etc. +_Disclaimer: The author of this tutorial is also the author of Datastore Entity._ -To model our Datastore entity as a Python class, we will use **Datastore-Entity** library. -Think of Datastore-Entity as an _ORM-like_ library for Firestore in Datastore mode. -_Disclaimer: I'm the author of datastore-entity. No Google affiliation._ +## Set up your local environment -### Set up your local environment NOTE: Projects that use the Datastore mode API require an active App Engine application. So you should already have App Engine and Datastore mode API enabled in your GCP project. From b19140c5312284194ea451a116f84525925e92d5 Mon Sep 17 00:00:00 2001 From: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com> Date: Thu, 24 Sep 2020 15:40:25 -0700 Subject: [PATCH 13/15] removing note file The notes have been incorporated into the main document. --- .../notes.txt | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 tutorials/using-flask-login-with-cloud-datastore/notes.txt diff --git a/tutorials/using-flask-login-with-cloud-datastore/notes.txt b/tutorials/using-flask-login-with-cloud-datastore/notes.txt deleted file mode 100644 index 47f9686ddc..0000000000 --- a/tutorials/using-flask-login-with-cloud-datastore/notes.txt +++ /dev/null @@ -1,19 +0,0 @@ -https://cloud.google.com/community/tutorials/building-flask-api-with-cloud-firestore-and-deploying-to-cloud-run - -Using Flask-Login Extension With Cloud Datastore -Prerequisites: - Familiarity with Flask (link to flask tutorial) - Familiarity with Cloud Datastore (link to datastore tutorial) - Familiarity with Flask-Login Extension (link to flask login tutorial) - -Datastore-Entity Tutorial with Flask Login -Flask Login requires a User class -Most Flask usage requires the use of models(with sql-alchemy) -Datastore Entity library helps you represent your datastore entity using Python classes(just like a relational database model) - -To easily represent your user entity as a class, we'll use a -library called datastore-entity(disclaimer: I authored this library. No Google affiliation) - -pip install flask-login -pip install datastore-entity - From fb636d418624bb7eaeaa5f1b71f70aea9113a0ba Mon Sep 17 00:00:00 2001 From: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com> Date: Thu, 24 Sep 2020 15:56:54 -0700 Subject: [PATCH 14/15] line edit through selecting service account role --- .../index.md | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index 6436e7aaad..adc056e90f 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -3,17 +3,17 @@ title: Using Flask-Login with Cloud Firestore in Datastore mode description: Represent your Cloud Datastore entity with a Python class and use this for Flask-Login user management. author: komlasapaty tags: Flask Framework, Python 3 -date_published: 2020-09-25 +date_published: 2020-09-24 --- In this tutorial, you implement user authentication using the popular Flask extension [Flask-Login](https://flask-login.readthedocs.io) with -[Firestore in Datastore mode](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. - -This tutorial is not meant to teach the fundamentals of Flask-Login or Firestore in Datastore mode. This tutorial demonstrates the use of Firestore in Datastore -mode (as opposed to any relational database) as the database backend for user authentication with Flask-Login. +[Firestore in Datastore mode](https://cloud.google.com/datastore/docs/datastore-api-tutorial) as the database backend. This tutorial demonstrates the use of +Firestore in Datastore mode (as opposed to any relational database) as the database backend for user authentication with Flask-Login. The use of Flask-Login should not force you to abandon the power of Firestore in Datastore mode in favor of a relational database. +This tutorial doesn't teach the fundamentals of Flask-Login or Firestore in Datastore mode. + Basic familiarity with the following is assumed: - Flask @@ -43,19 +43,21 @@ Flask-Login and WTForms. To model a Datastore entity as a Python class, this tutorial uses the [Datastore Entity library](https://datastore-entity.readthedocs.io). Think of Datastore Entity as an ORM-like library for Firestore in Datastore mode. -_Disclaimer: The author of this tutorial is also the author of Datastore Entity._ - +The author of this tutorial is also the author of the Datastore Entity library. ## Set up your local environment -NOTE: Projects that use the Datastore mode API require an active App Engine application. -So you should already have App Engine and Datastore mode API enabled in your GCP project. +Projects that use the Datastore mode API require an active App Engine application, so you should already have the App Engine and Datastore mode APIs enabled in +your Google Cloud project. + +### Create and download service account key + +To connect to your Google Cloud service from your local machine, you need the appropriate credentials (service account key). -As usual, to connect to your Google Cloud service from your local machine you need the appropriate credentials(service account key). -_Create and download service account key_ -- On the GCP console, go to **Main menu** -> **IAM & Admin** -> click **Service Account** on the left pane -> click **CREATE SERVICE ACCOUNT** at the top. -- Give the service account an appropriate name (eg. datastore-service-account). Enter an optional description below and click **CREATE** -- Under **Select a role** dropdown menu, type **Cloud Datastore owner** in the filter field, select it and click **CONTINUE** and click **DONE** on the next page. +1. In the Cloud Console, go to the [**Service accounts** page](https://console.cloud.google.com/iam-admin/serviceaccounts). +1. Click **Create service account**. +1. Give the service account an appropriate name, such as `datastore-service-account`, enter a description, and click **Create**. +1. Under **Select a role**, type `Cloud Datastore Owner` in the filter field, select **Cloud Datastore Owner**, click **Continue**, and click **Done**. - On the **Service Account** page, look for the service account you just created and click the overflow icon (vertical ellipsis) at the far right end. - Click **Create Key** from the dropdown menu and make sure **JSON** is selected as the **Key type**. - Click **CREATE**. This automatically downloads the service account JSON key to your local machine. Take note of the file name on the popup screen. @@ -80,13 +82,16 @@ You are now ready to connect to your Firestore in Datastore mode. ### Install required libraries -```bash -pip install flask flask-login datastore-entity -``` -NOTE: Installing datastore-entity installs the required/official client library for **Firestore in Datastore mode.** -### Create your Flask user model -Model your ```user``` entity. +Run the following command to install the required libraries: + + pip install flask flask-login datastore-entity + +Installing `datastore-entity` installs the required official client library for Firestore in Datastore mode. + +## Create your Flask user model + +Model your `user` entity. ```python from flask_login import UserMixin @@ -103,14 +108,16 @@ class User(DatastoreEntity, UserMixin): # other fields or methods go here... #def authenticated(self, password): # ... - ``` -### Implement the Flask 'login' view -Once the entity is modelled, you can fetch your user entity using familiar ORM pattern. -Use the methods provided on your model to fetch the user record. +## Implement the Flask login view + +After the entity is modeled, you can fetch your user entity using familiar ORM patterns. + +Use the methods provided on your model to fetch the user record. + +An example Flask-Login view: -An example flask login view. ```python from flask_login import login_user from models import User @@ -145,16 +152,19 @@ def login(): flash('Login or password is incorrect','error') return render_template("page/login.html", form=form) - ``` -### Conclusion -You can employ same approach to model any other Firestore in Datastore mode entity to persist/update data. -This technique can let Firestore in Datastore mode be a drop-in replacement for your relational database with little to no code changes. +## Conclusion + +You can employ the same approach to model any other Firestore in Datastore mode entity to persist and update data. + +This technique can let Firestore in Datastore mode be a drop-in replacement for your relational database with little to no code change. + + +## What's next -## Useful links -- [Firestore in Datastore mode Documentation](https://cloud.google.com/datastore) +- [Firestore in Datastore mode documentation](https://cloud.google.com/datastore) - [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/) -- [Flask-Login Documentation](https://flask-login.readthedocs.io) -- [Datastore-Entity Documentation](https://datastore-entity.readthedocs.io) -- [Flask-Login Tutorial Using Postgres](https://hackersandslackers.com/flask-login-user-authentication) +- [Flask-Login documentation](https://flask-login.readthedocs.io) +- [Datastore Entity documentation](https://datastore-entity.readthedocs.io) +- [Flask-Login tutorial using Postgres](https://hackersandslackers.com/flask-login-user-authentication) From 7988c6bfc5297f37024cb1eb0e64299c84ee723e Mon Sep 17 00:00:00 2001 From: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com> Date: Thu, 24 Sep 2020 19:36:56 -0700 Subject: [PATCH 15/15] finished first edit pass --- .../index.md | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/tutorials/using-flask-login-with-cloud-datastore/index.md b/tutorials/using-flask-login-with-cloud-datastore/index.md index adc056e90f..f588fb9667 100644 --- a/tutorials/using-flask-login-with-cloud-datastore/index.md +++ b/tutorials/using-flask-login-with-cloud-datastore/index.md @@ -58,28 +58,29 @@ To connect to your Google Cloud service from your local machine, you need the ap 1. Click **Create service account**. 1. Give the service account an appropriate name, such as `datastore-service-account`, enter a description, and click **Create**. 1. Under **Select a role**, type `Cloud Datastore Owner` in the filter field, select **Cloud Datastore Owner**, click **Continue**, and click **Done**. -- On the **Service Account** page, look for the service account you just created and click the overflow icon (vertical ellipsis) at the far right end. -- Click **Create Key** from the dropdown menu and make sure **JSON** is selected as the **Key type**. -- Click **CREATE**. This automatically downloads the service account JSON key to your local machine. Take note of the file name on the popup screen. +1. On the **Service accounts** page, look for the service account that you just created, click the button at the far right end of the row in the + **Actions** column, and select **Create key** in the dropdown menu. +1. Make sure that **JSON** is selected as the **Key type**, and click **Create**. + This automatically downloads the service account JSON key to your local machine. Take note of the filename. -_Configure your service account on your local machine_ -Point the environment variable **GOOGLE_APPLICATION_CREDENTIALS** to the location of the service account key you downloaded. -**Linux/maxOS** -```bash -export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" -``` -**Windows** -_With Powershell_ -``` -$env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" -``` -_With Command Prompt_ -``` -set GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" -``` -You are now ready to connect to your Firestore in Datastore mode. +### Configure your service account on your local machine + +Point the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the location of the service account key you downloaded: +* Linux or macOS: + + export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" + +* Windows, with Powershell: + + $env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" + +* Windows, with Command Prompt: + + set GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-acount-key.json" + +You are now ready to connect to your Firestore in Datastore mode. ### Install required libraries