FIX: OAuth now dynamically creates url to redirect based on .env instead of static url
This commit is contained in:
@@ -2,12 +2,18 @@ from authlib.integrations.flask_client import OAuth, OAuthError
|
|||||||
from flask import Blueprint, jsonify, session, redirect, request
|
from flask import Blueprint, jsonify, session, redirect, request
|
||||||
from blueprints.user import get_session_info_email
|
from blueprints.user import get_session_info_email
|
||||||
from database.database import Database
|
from database.database import Database
|
||||||
|
from dotenv import load_dotenv
|
||||||
from secrets import token_hex, token_urlsafe
|
from secrets import token_hex, token_urlsafe
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
oauth_bp = Blueprint("oauth", __name__)
|
oauth_bp = Blueprint("oauth", __name__)
|
||||||
google = None
|
google = None
|
||||||
|
|
||||||
|
from os import getenv
|
||||||
|
load_dotenv()
|
||||||
|
url = getenv("VITE_API_URL")
|
||||||
|
|
||||||
|
|
||||||
def init_oauth(app):
|
def init_oauth(app):
|
||||||
oauth = OAuth(app)
|
oauth = OAuth(app)
|
||||||
global google
|
global google
|
||||||
@@ -21,7 +27,7 @@ def init_oauth(app):
|
|||||||
api_base_url='https://www.googleapis.com/oauth2/v1/',
|
api_base_url='https://www.googleapis.com/oauth2/v1/',
|
||||||
userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',
|
userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',
|
||||||
server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
|
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')
|
@oauth_bp.route('/login/google')
|
||||||
@@ -34,7 +40,7 @@ def login_google():
|
|||||||
session["origin"] = request.args.get("next")
|
session["origin"] = request.args.get("next")
|
||||||
|
|
||||||
return google.authorize_redirect(
|
return google.authorize_redirect(
|
||||||
'http://127.0.0.1:8080/api/google_auth',
|
f'{url}/api/google_auth',
|
||||||
nonce=session['nonce']
|
nonce=session['nonce']
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -84,7 +90,7 @@ def google_auth():
|
|||||||
)
|
)
|
||||||
user_data = get_session_info_email(user_email)
|
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.clear()
|
||||||
session["username"] = user_data["username"]
|
session["username"] = user_data["username"]
|
||||||
session["user_id"] = user_data["user_id"]
|
session["user_id"] = user_data["user_id"]
|
||||||
|
|||||||
Reference in New Issue
Block a user