From e5e92db4003d45dbb7c3066b1a0f24a6c479a7fa Mon Sep 17 00:00:00 2001 From: Oscar <122336651@umail.ucc.ie> Date: Thu, 13 Feb 2025 12:49:43 +0000 Subject: [PATCH] FIX: OAuth now dynamically creates url to redirect based on .env instead of static url --- web_server/blueprints/oauth.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/web_server/blueprints/oauth.py b/web_server/blueprints/oauth.py index a40057d..c1554d1 100644 --- a/web_server/blueprints/oauth.py +++ b/web_server/blueprints/oauth.py @@ -2,12 +2,18 @@ from authlib.integrations.flask_client import OAuth, OAuthError from flask import Blueprint, jsonify, session, redirect, request from blueprints.user import get_session_info_email from database.database import Database +from dotenv import load_dotenv from secrets import token_hex, token_urlsafe from random import randint oauth_bp = Blueprint("oauth", __name__) google = None +from os import getenv +load_dotenv() +url = getenv("VITE_API_URL") + + def init_oauth(app): oauth = OAuth(app) global google @@ -21,7 +27,7 @@ def init_oauth(app): api_base_url='https://www.googleapis.com/oauth2/v1/', userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo', server_metadata_url='https://accounts.google.com/.well-known/openid-configuration', - redirect_uri="http://127.0.0.1:8080/api/google_auth" + redirect_uri=f"{url}/api/google_auth" ) @oauth_bp.route('/login/google') @@ -34,7 +40,7 @@ def login_google(): session["origin"] = request.args.get("next") return google.authorize_redirect( - 'http://127.0.0.1:8080/api/google_auth', + f'{url}/api/google_auth', nonce=session['nonce'] ) @@ -84,7 +90,7 @@ def google_auth(): ) user_data = get_session_info_email(user_email) - origin = session.pop("origin", "http://127.0.0.1:8080/") + origin = session.pop("origin", f"{url}") session.clear() session["username"] = user_data["username"] session["user_id"] = user_data["user_id"]