Skip to content

Commit 266b9a7

Browse files
committed
v1.0.35 - Flask Google OAuth
1 parent a9eda21 commit 266b9a7

File tree

2 files changed

+107
-48
lines changed

2 files changed

+107
-48
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## [1.0.35] 2023-05-18
4+
### Changes
5+
6+
- Update [Flask](https://docs.appseed.us/technologies/flask/) Section
7+
- [Flask Google OAuth](https://docs.appseed.us/technologies/flask/oauth-google/)
8+
39
## [1.0.34] 2023-05-12
410
### Changes
511

docs/technologies/flask/oauth-google.mdx

Lines changed: 101 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import SubHeading from "@site/src/components/SubHeading";
1212
1313
## Introduction to OAuth
1414

15-
OAuth which stands for Open Authorization is a protocol that allows users to grant third-party applications access to their data without giving away their passwords. OAuth works by giving each user a unique identifier and secret, which they can use to authorize applications to access their data.
15+
OAuth which stands for Open Authorization is a protocol that allows users to grant third-party applications access to their data without giving away their passwords.
16+
OAuth works by giving each user a unique identifier and secret, which they can use to authorize applications to access their data.
1617

17-
OAuth is a secure protocol that protects user data. The user's password is never shared with the website or app. The OAuth authorization server only grants access to the user's data if the user explicitly grants permission.
18+
OAuth is a secure protocol that protects user data. The user's password is never shared with the website or app.
19+
The OAuth authorization server only grants access to the user's data if the user explicitly grants permission.
1820

19-
OAuth is a widely used protocol. It is supported by many popular websites and apps, including Google, Facebook, Twitter, and Amazon. OAuth is a secure and convenient way for users to share their data with third-party applications.
21+
OAuth is a widely used protocol. It is supported by many popular websites and apps, including Google, Facebook, Twitter, and Amazon.
22+
OAuth is a secure and convenient way for users to share their data with third-party applications.
2023

2124
Here are some of the benefits of using OAuth:
2225

@@ -25,85 +28,108 @@ Here are some of the benefits of using OAuth:
2528
- Flexibility: OAuth is a flexible protocol that can be used to access a wide variety of data.
2629
- Interoperability: OAuth is a widely supported protocol that can be used with a wide variety of third-party applications.
2730

28-
If you are developing a website or app that needs to access user data, OAuth is a good option to consider. OAuth is a secure, convenient, and flexible protocol that can help you to protect user data and make it easy for users to share their data with your application.
31+
If you are developing a website or app that needs to access user data, OAuth is a good option to consider.
32+
OAuth is a secure, convenient, and flexible protocol that can help you to protect user data and make it easy for users to share their data with your application.
33+
34+
<br />
2935

3036
## Creating a Google OAuth Client ID
3137

3238
To create a Google OAuth Client ID:
39+
3340
- You will need to create a Google Cloud Platform project. Open Google Cloud Platform [here](https://console.cloud.google.com/)
3441
- Once you have created a project, you can go to the API Credentials page and click the "Create Credentials" button.
3542
- Select "OAuth Client ID" and then "Web application".
3643
- Enter a name for your application and click the "Create" button. You will then be given a Client ID and Client Secret.
3744

45+
<br />
46+
3847
## Setting up Flask application
48+
3949
Flask is a microframework for Python web development. It is a simple, flexible, and extensible framework that can be used to create a wide variety of web applications.
4050

4151
This tutorial will focus on setting up OAuth for an existing Flask template. The template is hosted on Github and will be modified to accept user authorization using OAuth.
4252

43-
- Open the terminal and clone the repository using the command
53+
> Open the terminal and clone the repository using the command
54+
4455
```bash
4556
$ git clone https://github.com/app-generator/flask-google-oauth
4657
```
4758

48-
- Create a virtual environment to install the packages needed for the application. From the terminal execute the command
59+
> Create a virtual environment to install the packages needed for the application. From the terminal execute the command
4960
5061
On `Linux/MacOS`
62+
5163
```bash
5264
$ cd flask-google-oauth
53-
flask-google-oauth$ virtualenv venv
54-
flask-google-oauth$ source venv/bin/activate
55-
(venv) flask-google-oauth$
65+
$ virtualenv venv
66+
$ source venv/bin/activate
67+
(venv) $
5668
```
5769

5870
On `Windows`
71+
5972
```bash
6073
$ cd flask-google-oauth
61-
flask-google-oauth$ virtualenv venv
62-
flask-google-oauth$ ./venv/Scripts/activate
63-
(venv) flask-google-oauth$
74+
$ virtualenv venv
75+
$ ./venv/Scripts/activate
76+
(venv) $
6477
```
78+
6579
The virtual environment has been created and activated, now we can start installing packages needed for the project.
6680

67-
- Install the packages needed for the template from the terminal using `pip`
81+
> Install the packages needed for the template from the terminal using `pip`
82+
6883
```bash
69-
(venv) flask-google-oauth$ pip install -r requirements.txt
84+
(venv) $ pip install -r requirements.txt
7085
```
7186

72-
- We will be adding some environmental variables to aid in the running of the web application
87+
> We will be adding some environmental variables to aid in the running of the web application
88+
7389
On `Linux/MacOS`:
90+
7491
```bash
75-
(venv) flask-google-oauth$ export FLASK_APP=run.py
76-
(venv) flask-google-oauth$ export FLASK_ENV=development
77-
(venv) flask-google-oauth$ export GOOGLE_OAUTH_CLIENT_SECRET=<google_client_secret>
78-
(venv) flask-google-oauth$ export GOOGLE_OAUTH_CLIENT_ID=<google_client_id>
92+
(venv) $ export FLASK_APP=run.py
93+
(venv) $ export FLASK_ENV=development
94+
(venv) $ export GOOGLE_OAUTH_CLIENT_SECRET=<google_client_secret>
95+
(venv) $ export GOOGLE_OAUTH_CLIENT_ID=<google_client_id>
7996
```
8097

8198
On Windows:
99+
82100
```bash
83-
(venv) flask-google-oauth$ # CMD
84-
(venv) flask-google-oauth$ set FLASK_APP=run.py
85-
(venv) flask-google-oauth$ set FLASK_ENV=development
86-
(venv) flask-google-oauth$ set GOOGLE_CLIENT_SECRET=<google_client_secret>
87-
(venv) flask-google-oauth$ set GOOGLE_CLIENT_ID=<google_client_id>
88-
(venv) flask-google-oauth$
89-
(venv) flask-google-oauth$ # Powershell
90-
(venv) flask-google-oauth$ $env:FLASK_APP = ".\run.py"
91-
(venv) flask-google-oauth$ $env:FLASK_ENV = "development"
92-
(venv) flask-google-oauth$ $env:GOOGLE_OAUTH_CLIENT_SECRET = "<google_client_secret>"
93-
(venv) flask-google-oauth$ $env:GOOGLE_OAUTH_CLIENT_ID = "<google_client_id>"
101+
(venv) $ # CMD
102+
(venv) $ set FLASK_APP=run.py
103+
(venv) $ set FLASK_ENV=development
104+
(venv) $ set GOOGLE_CLIENT_SECRET=<google_client_secret>
105+
(venv) $ set GOOGLE_CLIENT_ID=<google_client_id>
106+
(venv) $
107+
(venv) $ # Powershell
108+
(venv) $ $env:FLASK_APP = ".\run.py"
109+
(venv) $ $env:FLASK_ENV = "development"
110+
(venv) $ $env:GOOGLE_OAUTH_CLIENT_SECRET = "<google_client_secret>"
111+
(venv) $ $env:GOOGLE_OAUTH_CLIENT_ID = "<google_client_id>"
94112
```
95113

114+
<br />
115+
96116
## Installing Flask-Dance
97-
Flask-Dance is a Flask extension that makes it easy to add OAuth authentication to your Flask apps. Flask-Dance provides a single, consistent API for working with OAuth providers, making it easy to add authentication to your app without having to learn the specific OAuth protocol for each provider.
117+
118+
Flask-Dance is a Flask extension that makes it easy to add OAuth authentication to your Flask apps.
119+
Flask-Dance provides a single, consistent API for working with OAuth providers, making it easy to add authentication to your app without having to learn the specific OAuth protocol for each provider.
98120

99121
Flask-Dance supports a wide variety of OAuth providers, including Google, Facebook, Twitter, GitHub, Bitbucket, Instagram, Reddit, Spotify, and many more
100122

101-
- Install `flask-dance` by executing the command on your terminal
123+
> Install `flask-dance` via PIP
124+
102125
```
103-
(venv) flask-google-oauth$ pip install flask-dance
126+
(venv) $ pip install flask-dance
104127
```
105128

106-
## Configuring Flask-Dance with Google OAuth
129+
<br />
130+
131+
## Configuring Flask-Dance for Google OAuth
132+
107133
Some changes will be made to the project files to add an authentication mechanism using Google OAuth. Below is the current folder structure of the project.
108134
```bash
109135
.
@@ -119,7 +145,9 @@ Some changes will be made to the project files to add an authentication mechanis
119145
├── requirements.txt
120146
└── run.py
121147
```
122-
- We will be creating an authentication schema that will be used to save authentication tokens when a user login using Google authentication. Make the following changes to `apps/authentication/models.py`
148+
149+
We will be creating an authentication schema that will be used to save authentication tokens when a user login using Google authentication.
150+
Make the following changes to `apps/authentication/models.py`
123151

124152
```py
125153
# apps/authentication/models.py
@@ -147,7 +175,9 @@ class OAuth(OAuthConsumerMixin, db.Model): # <-- NEW class
147175
user = db.relationship(Users)
148176
```
149177

150-
- We will be creating an `oauth` module to handle the signup and login using Google authentication. Create a file `oauth.py` in the `apps/authentication` folder and add the following code
178+
We will be creating an `oauth` module to handle the signup and login using Google authentication.
179+
Create a file `oauth.py` in the `apps/authentication` folder and add the following code
180+
151181
```py
152182
# apps/authentication/oauth.py
153183
import os
@@ -196,9 +226,11 @@ def google_logged_in(blueprint, token):
196226

197227
login_user(user)
198228
```
229+
199230
We have created a blueprint that makes use of Google's authentication. We connected the blueprint to the `OAuth` schema.
200231

201-
- Next step is to register the blueprint to our Flask application to make it accessible from the browser route. Update `apps/__init__.py` to register the blueprint
232+
Next step is to register the blueprint to our Flask application to make it accessible from the browser route. Update `apps/__init__.py` to register the blueprint
233+
202234
```py
203235
# apps/__init__.py
204236
...
@@ -220,7 +252,8 @@ def create_app():
220252
return app
221253
```
222254

223-
- Next step is creating a route for the google blueprint that will handle requests for the authentication. Update `apps/authentication/routes.py` with the following code
255+
Next step is creating a route for the google blueprint that will handle requests for the authentication. Update `apps/authentication/routes.py` with the following code
256+
224257
```py
225258
# apps/authentication/routes.py
226259
...
@@ -238,35 +271,55 @@ def login_google():
238271
```
239272
Now requests to `login/google` route will be handled by this route. From your terminal you can run the command `flask routes` to see all the routes present in the application.
240273

241-
- Start up the application from the terminal using the command
274+
Start up the application from the terminal using the command
275+
242276
```bash
243-
(venv) flask-google-oauth$ flask run
277+
(venv) $ flask run
244278
```
245-
From your browser visit [`127.0.0.1:5000/login/google`](http://127.0.0.1:5000/login/google). Once you have logged in, you will be able to access protected resources in your Flask app.
246279

247-
## Adding Signin button to login page
280+
From your browser visit [`127.0.0.1:5000/login/google`](http://127.0.0.1:5000/login/google).
281+
Once you have logged in, you will be able to access protected resources in your Flask app.
282+
283+
<br />
284+
285+
## Adding SignIN button (Login Page)
286+
248287
We will be adding a link to the login page to simplify the signin process for the user.
249288

250-
- Add the following code to `apps/templates/accounts/login.html` on line 37
289+
Add the following code to `apps/templates/accounts/login.html` on `Line 37`
290+
251291
```html
252292
<!--apps/templates/accounts/login.html-->
253293
...
254294
<div class="card-body">
255295
<a href='login/google' class='btn btn-primary'>Sign in with Google</a>
256296
...
257297
```
258-
This adds a link that points to the authorization route. With this, users can signin to the web application using google authentication wihtout having to change the URL from the browser, making for a better user experience.
298+
299+
This adds a link that points to the authorization route.
300+
With this, users can signin to the web application using google authentication wihtout having to change the URL from the browser, making for a better user experience.
301+
302+
<br />
259303

260304
## Conclusion
261-
In conclusion, this article has provided an overview of OAuth and its importance in modern web applications. We explored the process of creating a Google OAuth Client ID, which is essential for enabling user authentication and authorization using Google accounts. Additionally, we discussed the steps involved in setting up a Flask application, a popular Python web framework, to handle OAuth authentication.
262305

263-
To facilitate OAuth functionality in our Flask application, we installed and configured Flask-Dance, a Flask extension that simplifies the integration of OAuth providers. Specifically, we focused on configuring Flask-Dance with Google OAuth, allowing users to authenticate with their Google accounts in our application.
306+
In conclusion, this article has provided an overview of OAuth and its importance in modern web applications.
307+
We explored the process of creating a Google OAuth Client ID, which is essential for enabling user authentication and authorization using Google accounts.
308+
Additionally, we discussed the steps involved in setting up a Flask application, a popular Python web framework, to handle OAuth authentication.
309+
310+
To facilitate OAuth functionality in our Flask application, we installed and configured Flask-Dance, a Flask extension that simplifies the integration of OAuth providers.
311+
Specifically, we focused on configuring Flask-Dance with Google OAuth, allowing users to authenticate with their Google accounts in our application.
264312

265-
By adding the "Sign in with Google" button to your login page, you provide users with a convenient and secure method to access your Flask application using their Google accounts. This not only simplifies the login process but also enhances the user experience and increases user engagement.
313+
By adding the "Sign in with Google" button to your login page, you provide users with a convenient and secure method to access your Flask application using their Google accounts.
314+
This not only simplifies the login process but also enhances the user experience and increases user engagement.
266315

267-
By following the steps outlined in this article, developers can leverage the power of OAuth and Google OAuth specifically to enable secure user authentication and authorization in their Flask applications. This process not only enhances the user experience but also provides a seamless integration with widely used social media and identity platforms.
316+
By following the steps outlined in this article, developers can leverage the power of OAuth and Google OAuth specifically to enable secure user authentication and authorization in their Flask applications.
317+
This process not only enhances the user experience but also provides a seamless integration with widely used social media and identity platforms.
318+
319+
<br />
268320

269321
## Resources
322+
270323
- 👉 [Flask-Dance](https://flask-dance.readthedocs.io/en/latest/) Documentation
271324
- 👉 [Google Cloud Platform](https://console.cloud.google.com/) - the official website
272325
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord

0 commit comments

Comments
 (0)