Skip to content

Commit

Permalink
Fixes + Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarakalmehairbi committed Jul 30, 2023
1 parent 1594960 commit 51e2e5a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
19 changes: 13 additions & 6 deletions docs/Examples.advanced_example_3_toui_with_google_sign_in.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ ToUI with Google sign in

ToUI can be used with Google sign in. This example shows how to use Google sign in with ToUI.
Make sure to create a Google app first. Also, add the following as an authorized redirect URI to your Google app:
`https://<your-domain>/toui-google-sign-in`
``https://<your-domain>/toui-google-sign-in``

One way to create a Google app is through Google Firebase. Perhaps check it out.
One way to create a Google app is through Google Firebase. Perhaps check it out. Note that you will need to access
Google API console nevertheless to enter the authorized redirect URI: ``https://<your-domain>/toui-google-sign-in``

This example uses the HTML file "test6.html":

Expand All @@ -28,9 +29,15 @@ Python code:

.. code-block:: python
import sys
sys.path.append("..")
import os
from toui import Website, Page
# Get these from your google app.
GOOGLE_CLIENT_ID = os.environ['GOOGLE_CLIENT_ID']
GOOGLE_CLIENT_SECRET = os.environ['GOOGLE_CLIENT_SECRET']
# Create app
app = Website(__name__, assets_folder="assets", secret_key="some secret key")
app.add_user_database_using_sql(f"sqlite:///{os.getcwd()}/.user_database.db")
Expand All @@ -41,13 +48,13 @@ Python code:
# Create functions that will be called from HTML
def get_user_info():
pg = app.get_user_page()
if app.get_current_user():
pg.get_element("user-info").set_content(f"User: {app.get_current_user().username}")
if app.is_signed_in():
pg.get_element("user-info").set_content(f"User: {app.get_current_user_data('username')}")
def sign_in():
"""Sign in using Google."""
app.sign_in_using_google(client_id=os.environ['GOOGLE_CLIENT_ID'],
client_secret=os.environ['GOOGLE_CLIENT_SECRET'],
app.sign_in_using_google(client_id=GOOGLE_CLIENT_ID,
client_secret=GOOGLE_CLIENT_SECRET,
after_auth_url="/")
# Connect functions to elements
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ToUI
====

Version: v3.0.3
Version: v3.0.4

.. include:: ../README.md
:parser: myst_parser.sphinx_
Expand Down
19 changes: 13 additions & 6 deletions examples/advanced_example_3_toui_with_google_sign_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
ToUI can be used with Google sign in. This example shows how to use Google sign in with ToUI.
Make sure to create a Google app first. Also, add the following as an authorized redirect URI to your Google app:
`https://<your-domain>/toui-google-sign-in`
``https://<your-domain>/toui-google-sign-in``
One way to create a Google app is through Google Firebase. Perhaps check it out.
One way to create a Google app is through Google Firebase. Perhaps check it out. Note that you will need to access
Google API console nevertheless to enter the authorized redirect URI: ``https://<your-domain>/toui-google-sign-in``
This example uses the HTML file "test6.html":
Expand All @@ -26,9 +27,15 @@
Python code:
"""
import sys
sys.path.append("..")
import os
from toui import Website, Page

# Get these from your google app.
GOOGLE_CLIENT_ID = os.environ['GOOGLE_CLIENT_ID']
GOOGLE_CLIENT_SECRET = os.environ['GOOGLE_CLIENT_SECRET']

# Create app
app = Website(__name__, assets_folder="assets", secret_key="some secret key")
app.add_user_database_using_sql(f"sqlite:///{os.getcwd()}/.user_database.db")
Expand All @@ -39,13 +46,13 @@
# Create functions that will be called from HTML
def get_user_info():
pg = app.get_user_page()
if app.get_current_user():
pg.get_element("user-info").set_content(f"User: {app.get_current_user().username}")
if app.is_signed_in():
pg.get_element("user-info").set_content(f"User: {app.get_current_user_data('username')}")

def sign_in():
"""Sign in using Google."""
app.sign_in_using_google(client_id=os.environ['GOOGLE_CLIENT_ID'],
client_secret=os.environ['GOOGLE_CLIENT_SECRET'],
app.sign_in_using_google(client_id=GOOGLE_CLIENT_ID,
client_secret=GOOGLE_CLIENT_SECRET,
after_auth_url="/")

# Connect functions to elements
Expand Down
2 changes: 1 addition & 1 deletion toui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .structure import ToUIBlueprint
from . import exceptions

__version__ = "v3.0.3"
__version__ = "v3.0.4"
9 changes: 6 additions & 3 deletions toui/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,6 @@ def _sign_in_using_google(self):
client_id = self._google_data['client_id']
client_secret = self._google_data['client_secret']
scope = request.args.get("scope")
for s in scope.split(" "):
if not s.startswith("https://www.googleapis.com/auth/"):
raise Exception("Invalid scope. Scope should start with `https://www.googleapis.com/auth/`.")
redirect_uri = request.base_url
response_type = "code"
access_type = request.args.get("access_type")
Expand Down Expand Up @@ -1017,6 +1014,12 @@ def _sign_in_using_google(self):
self.signup_user(email=email, username=username, password=None)
return redirect(after_auth_url)
else:
# Validating scope
for s in scope.split(" "):
if not s.startswith("https://www.googleapis.com/auth/"):
raise ValueError(f"Invalid scope. Scope should start with `https://www.googleapis.com/auth/`. However your scope is `{s}`")

# Creating redirect_to
redirect_to = f"https://accounts.google.com/o/oauth2/v2/auth?" \
f"client_id={client_id}" \
f"&response_type={response_type}" \
Expand Down

0 comments on commit 51e2e5a

Please sign in to comment.