From 18bb476ce52d32ed2d62f8772ac34bf825fc4d76 Mon Sep 17 00:00:00 2001 From: Suheyla Dilsat Karaarslan Date: Sun, 21 Jan 2024 21:06:01 +0100 Subject: [PATCH 1/2] updates requirements.txt with Jina library --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index efc00d4..3d62b8a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,8 @@ h11==0.14.0 httptools==0.6.1 idna==3.6 itsdangerous==2.1.2 +Jinja2==3.1.3 +MarkupSafe==2.1.4 pydantic==2.5.3 pydantic_core==2.14.6 python-dotenv==1.0.0 From 1d1868ea32227e5eff41bf19fca6d93328534bcf Mon Sep 17 00:00:00 2001 From: Suheyla Dilsat Karaarslan Date: Sun, 21 Jan 2024 21:48:22 +0100 Subject: [PATCH 2/2] adds the jinja template and updates the index endpoint to render html template --- main.py | 20 ++++++++------------ templates/index.html | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 templates/index.html diff --git a/main.py b/main.py index f2b622b..fa8c565 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,10 @@ from fastapi.middleware.cors import CORSMiddleware from starlette.middleware.sessions import SessionMiddleware from session_cache_handler import SessionCacheHandler +from fastapi.templating import Jinja2Templates + +# Jinja2 template engine +templates = Jinja2Templates(directory="templates") load_dotenv('.env') @@ -34,20 +38,12 @@ @app.get("/") def index(request: Request): - # Get spotify token info from session - spotify_token_info = request.session.get("spotify_token_info") # Get the SpotifyOAuth object auth_manager = _get_spotify_oauth(request) - # Check if token is valid - if auth_manager.validate_token(spotify_token_info): - # Use access token to create Spotify client - spotify_client = spotipy.Spotify(auth=spotify_token_info['access_token']) - - # Example: Fetch user profile data - user_data = spotify_client.me() - return user_data - - return {"Hello": "World"} + # Check if we have a cached token + is_authenticated = bool(auth_manager.get_cached_token()) + # Render index.html template + return templates.TemplateResponse("index.html", {"request": request, "is_authenticated": is_authenticated}) @app.get("/login") def login(request: Request): diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..b9245cc --- /dev/null +++ b/templates/index.html @@ -0,0 +1,43 @@ + + + + + + +
+
+
+
+
+

Welcome to the Home Page

+ {% if is_authenticated %} +
+ {% else %} + + {% endif %} +
+ + +
+
+ +
+
+
+
+
+
+
+ +