This commit is contained in:
JSC
2025-06-28 19:19:54 +02:00
parent ceafed9108
commit 85f420d2f7
5 changed files with 48 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ from datetime import timedelta
from flask import Flask
from flask_jwt_extended import JWTManager
from flask_cors import CORS
from app.services.auth_service import AuthService
from app.database import init_db
@@ -33,6 +34,13 @@ def create_app():
app.config["JWT_ACCESS_COOKIE_PATH"] = "/api/"
app.config["JWT_REFRESH_COOKIE_PATH"] = "/api/auth/refresh"
# Initialize CORS
CORS(app,
origins=["http://localhost:3000"], # Frontend URL
supports_credentials=True, # Allow cookies
allow_headers=["Content-Type", "Authorization"],
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
# Initialize JWT manager
jwt = JWTManager(app)

View File

@@ -19,10 +19,27 @@ def login_oauth(provider):
@bp.route("/callback/<provider>")
def callback(provider):
"""Handle OAuth callback from specified provider."""
from flask import redirect, make_response
try:
return auth_service.handle_callback(provider)
auth_response = auth_service.handle_callback(provider)
# If successful, redirect to frontend dashboard with cookies
if auth_response.status_code == 200:
redirect_response = make_response(redirect("http://localhost:3000/dashboard"))
# Copy all cookies from the auth response
for cookie in auth_response.headers.getlist('Set-Cookie'):
redirect_response.headers.add('Set-Cookie', cookie)
return redirect_response
else:
# If there was an error, redirect to login with error
return redirect("http://localhost:3000/login?error=oauth_failed")
except Exception as e:
return {"error": str(e)}, 400
error_msg = str(e).replace(' ', '_').replace('"', '')
return redirect(f"http://localhost:3000/login?error={error_msg}")
@bp.route("/providers")

View File

@@ -16,9 +16,13 @@ class TokenService:
"email": user_data["email"],
"name": user_data["name"],
"picture": user_data.get("picture"),
"role": user_data.get("role"),
"is_active": user_data.get("is_active"),
"provider": user_data.get("provider"),
"providers": user_data.get("providers", []),
},
)
def generate_refresh_token(self, user_data: dict[str, Any]) -> str:
"""Generate a refresh token for the user."""
return create_refresh_token(identity=user_data["id"])
return create_refresh_token(identity=user_data["id"])